mercredi 29 novembre 2017

In laravel eloquent create method i am not able to save the data in database

I have Three model -> Language-> Article-> Category.. Article table has two Foreign Key category_id & Language_id, Language-Model has "One to Many" Relationship with Article-Model, Similarly Category-Model has "One to Many" Relationship with Article-Model.

Category- Model

class Category extends Model
            { 

            protected $fillable = [
                 'category_name', 
            ];

            public function articles(){

                return $this->hasMany('App\Article');
             }
           }

Language-Model

class Language extends Model
    {
         protected $fillable = [
            'language_name'
        ];

         public function articles(){

            return $this->hasMany('App\Article');
        }

    }

Article _model

class Article extends Model
{
 protected $fillable = [

   'language_id','category_id','category_name','article_title',
   'article_desc','source_from','source_url','video_link',
   'audio_url','author_name','article_image_url','like_count'
    ];

    public function languages(){

        return $this->belongsTo('App\Language');
    }

    public function categories(){

        return $this->belongsTo('App\Category');
    }

}

How to insert in the database using laravel Eloquent

 $Article = new Article (
                 [
                   'article_image_url' => $img_url ,
                   'audio_url' => $audio_url,
                   'category_name'=>$category_name ,
                   'article_title'=>$request->article_title,
                   'article_desc'=>$request->article_desc,
                   'source_from'=>$request->source_from,
                    'source_url'=>$request->source_url,
                    'video_link'=>$request->video_link,
                    'author_name'=>$request->author_name,
                    'like_count'=>'0',

                  ]
            );

            $language = new Language;
            $category = new Category;
            $language->articles()->save($Article);

since language_id doesn't have a default value it is foreign key please help me out with this



via Chebli Mohamed

mardi 28 novembre 2017

output only updated after second form submit

I have a from with an input that filters a database query by a string. Often, but strangely enough not always, I have to send this form twice to get updated data after changing the filter string. The filter string is updated upon sending the form in because I flash the request. So that part works.

In between sending the forms there is also an ajax call to the same route (json return). Maybe that is mixing something up with the csrf tokens?

Why is the data not updated? It seems it is cached? I followed the basic tutorial. (I know the code is not perfect, it's my first laravel project)

Here is the route: (I change the real name of the model to "Model")

Route::get('myRoute', ['as' => 'myRoute', function (\Illuminate\Http\Request $request) {
    $request->flash();
    $model = new \App\Model();

    if($request->accepts('text/html')) {
        if($request->has('filter')) {
            $searchString = $request->filter;
        } else {
            $searchString = '';
        }
        return view('myView', [
            'searchString' => $searchString,
            'models' => $model->getModels()
        ]);
    } elseif($request->accepts('application/json')) {
        return response()->json($model->getModels());
    }
}]);

And here the relevant part of the Model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Model extends Model
{
    public function getRequests()
    {

        if (request()->has('filter')) {
            return $this->searchByFilter();
        }

        if (request()->has('email')) {
            return $this->searchByEmail();
        }
    }

    private function searchByFilter()
    {
        $filter = request();
        if (is_null($filter)) {
            return $this->orderBy('created', 'asc')->get();
        }
        return $this->selectRaw(
            "*, MATCH(
                    something
                ) AGAINST ('{$filter}' IN NATURAL LANGUAGE MODE) as relevance")
            ->whereRaw("MATCH(
                            something
                        ) AGAINST ('{$filter}' IN NATURAL LANGUAGE MODE)")
            ->orderBy('relevance', 'desc')
            ->limit(25)
            ->get();
    }
}

The view is very simple:

@if (!is_null($models))
    @forelse ($models as $key => $model)
        <div></div
    @empty
        <p>no results.</p>
    @endforelse
@else
    <p>please filter something.</p>
@endif



via Chebli Mohamed

lundi 27 novembre 2017

Dynamic Bootstrap-date-picker-sandbox highlight specific date

Currently in a Calendar i am setting background color on static date. In my for loop i am getting array:

// BookeData loop 
@if(!empty($BookeData))
        @foreach($BookeData as $key => $value)
            // getting single venue booked date 
            // Flag : 1 - for 1st half, 2 - for 2nd half and 3 - for full day 
        @endforeach
@endif

On below script i want to set it dynamically , on a booked date and $value->session == 1 then apply gradient css red and green..if session value 2 vice versa css apply..if 3 apply full colour css.

My Script which is working on static date is

<script type="text/javascript">
var booked_date = ["27-11-2017","27-11-2017"]; 
$("#datepicker").datepicker({
 format: "dd/mm/yyyy",
 autoclose: true,

    beforeShowDay: function(date){
    var d = date;
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months are zero based
    var curr_year = d.getFullYear();
    var formattedDate = curr_date + "-" + curr_month + "-" + curr_year
    if ($.inArray(formattedDate, booked_date) != -1){
       return {
          classes: 'activeClass'
       };
     }
  return;
}
});

Active class css

.activeClass{
  background: -webkit-linear-gradient(red, yellow); 
  background: -o-linear-gradient(red, yellow); 
  background: -moz-linear-gradient(red, yellow);  
  background: linear-gradient(red, yellow);  

}

How can i do this dynamically on my array.!! Thanks in advance.



via Chebli Mohamed

L5.1 - Command with localization not working (not in Ubuntu but yes on Mac)

I have a command called SendReminders, when I change the locale with App::setLocale('locale') it looks like is working because when I do App::getLocale() returns the correct one, but when I call for a translation returns always the default localization.

This is my SendReminders class:

<?php namespace App\Console\Commands;

use Illuminate\Console\Command;

use App;
use Exception;
use Storage;
use Lang;

class SendReminders extends Command
{

    protected $signature = 'send:reminders';

    public function __construct(){
        parent::__construct();
    }

    public function handle(){

        App::setLocale('EN');
        echo App::getLocale()."\n"; // <= Shows correctly 'EN'
        echo Lang::get('general.contact')."\n"; // <= Shows correctly 'Contact'

        App::setLocale('DE');
        echo App::getLocale()."\n"; // <= Shows correctly 'DE'
        echo Lang::get('general.contact')."\n"; // <= Doesn't show the correct value

    }

}

I'm missing something to make the localization work?

EDIT:

Some estrange behavior is happening because on my Mac is working but on Linux (Ubuntu) is not, looks like is not finding my folder /resources/lang/de/



via Chebli Mohamed

vendredi 24 novembre 2017

i am trying to show affiliate program banner in laravel 5.1 with widgetify liabrary, but it is not showing banner

I am just trying to show banner of affiliate program in laravel 5.1. But its not showing that banner.As per google search i found widgetify package and implemented it successfully but still unable to view banner with this html and script code.

This is my code to show banner:

<div data-WRID="WRID-151136341816463865" data-widgetType="productBanner" data-class="affiliateAdsByFlipkart" height="240px" width="120px"></div>
<script async src="//affiliate.flipkart.com/affiliate/widgets/FKAffiliateWidgets.js"></script>

and when i am print it in wordpress its working fine.



via Chebli Mohamed

jeudi 23 novembre 2017

response with appropriate mime type requested with accept

Say I have a route:

Route::get('list',...);

If I call that route with Accept: text/html it should return a view with all the blade hoopla. If I call that route with Accept: application/json it should return json, Accept: application/xml it will return xml. And so on...

How do I realise that with Laravel 5.1?



via Chebli Mohamed

mercredi 22 novembre 2017

post image to webservice using cURL in laravel

I want post $data and $fileData to the webservice using cURL

Any one can help.

This is my code :

$data = $request->bodyData;
$fileData = image file;

$postfields = array();
$postfields['data'] = $data;
$postfields['fileData'] = $fileData;


 $ch = curl_init();

 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $output = curl_exec($ch);

 curl_close($ch);



via Chebli Mohamed