Laravel catch Eloquent “Unique” field error

I’m assuming you use MySQL, it’s probably different for other systems Okay first, the error code for duplicate entry is 1062. And here’s how you retrieve the error code from the exception: catch (Illuminate\Database\QueryException $e){ $errorCode = $e->errorInfo[1]; if($errorCode == 1062){ // houston, we have a duplicate entry problem } }

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

Using namespaces in Laravel 4

Namespacing is pretty easy once you get that hang of it. Take the following example: app/models/File.php namespace App\Models; class File { public function someMethodThatGetsFiles() { } } app/controllers/FileController.php namespace App\Controllers; use App\Models\File; class FileController { public function someMethod() { $file = new File(); } } Declare the Namespace: namespace App\Controllers; Remember, once you’ve put a … Read more

Laravel Eager Loading – Load only specific columns

Make use of the select() method: public function car() { return $this->hasOne(‘Car’, ‘id’)->select([‘owner_id’, ’emailid’, ‘name’]); } Note: Remember to add the columns assigned to the foreign key matching both tables. For instance, in my example, I assumed a Owner has a Car, meaning that the columns assigned to the foreign key would be something like … 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