Laravel Eloquent Ignore Casing

Use whereRaw with parameter binding to sanitize your whereRaw statement: $term = strtolower($vars[‘language’]); Item::whereRaw(‘lower(language) like (?)’,[“%{$term}%”])->get(); Prev answer In some dabases you can use operator ilike in your where. For example Item::where(‘language’, ‘ilike’, $vars[‘language’])->get(); All available operators are: protected $operators = array( ‘=’, ‘<‘, ‘>’, ‘<=’, ‘>=’, ‘<>’, ‘!=’, ‘like’, ‘not like’, ‘between’, ‘ilike’, ‘&’, … Read more

A __construct on an Eloquent Laravel Model

You need to change your constructor to: public function __construct(array $attributes = array()) { parent::__construct($attributes); $this->directory = $this->setDirectory(); } The first line (parent::__construct()) will run the Eloquent Model‘s own construct method before your code runs, which will set up all the attributes for you. Also the change to the constructor’s method signature is to continue … Read more

Find or Create with Eloquent

Below is the original accepted answer for: Laravel-4 There is already a method findOrFail available in Laravel and when this method is used it throws ModelNotFoundException on fail but in your case you can do it by creating a method in your model, for example, if you have a User model then you just put … Read more

How can I query raw via Eloquent?

You may try this: // query can’t be select * from table where Model::select(DB::raw(‘query’))->get(); An Example: Model::select(DB::raw(‘query’)) ->whereNull(‘deleted_at’) ->orderBy(‘id’) ->get(); Also, you may use something like this (Using Query Builder): $users = DB::table(‘users’) ->select(DB::raw(‘count(*) as user_count, status’)) ->where(‘status’, ‘<>’, 1) ->groupBy(‘status’) ->get(); Also, you may try something like this (Using Query Builder): $users = DB::select(‘select … Read more

Laravel: Returning the namespaced owner of a polymorphic relation

There are 2 easy ways – one below, other one in @lukasgeiter’s answer as proposed by Taylor Otwell, which I definitely suggest checking as well: // app/config/app.php or anywhere you like ‘aliases’ => [ … ‘MorphOrder’ => ‘Some\Namespace\Order’, ‘MorphStaff’ => ‘Maybe\Another\Namespace\Staff’, … ] // Staff model protected $morphClass=”MorphStaff”; // Order model protected $morphClass=”MorphOrder”; done: $photo … Read more

First Or Create

firstOrCreate() checks for all the arguments to be present before it finds a match. If not all arguments match, then a new instance of the model will be created. If you only want to check on a specific field, then use firstOrCreate([‘field_name’ => ‘value’]) with only one item in the array. This will return the … Read more

Laravel save / update many to many relationship

tldr; Use sync with 2nd param false Many-to-many relationship is belongsToMany on both models: // Task model public function users() { return $this->belongsToMany(‘User’, ‘user_tasks’); // assuming user_id and task_id as fk } // User model public function tasks() { return $this->belongsToMany(‘Task’, ‘user_tasks’); } In order to add new relation use attach or sync. Difference between … Read more