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

I’m getting error “Class ‘Predis\Client’ not found” in Laravel 5.2

First download the REDIS to your system (if you haven’t already installed it). Go to the folder where you have downloaded the redis and run this command: cd your-redis-folder-name make Go to your project directory and install composer: composer require predis/predis Go to your .env file and add Queue driver: QUEUE_DRIVER=redis use Mail::queue() to send … Read more

Add sql table column before or after specific other column – by migrations in Laravel 4.1

Yes. Create a new migration using php artisan migrate:make update_users_table. Then use the table command as follows (if you’re using MySQL!): Schema::table(‘users’, function($table) { $table->string(‘phone_nr’)->after(‘id’); }); Once you’ve finished your migration, save it and run it using php artisan migrate and your table will be updated. Documentation: https://laravel.com/docs/4.2/migrations#column-modifiers

How to fix the error “You may need an appropriate loader to handle this file type”

as your using laravel-mix 6 it have different config for webpack.mix.js webpack.mix.js use .vue() const mix = require(‘laravel-mix’); /* |————————————————————————– | Mix Asset Management |————————————————————————– | | Mix provides a clean, fluent API for defining some Webpack build steps | for your Laravel applications. By default, we are compiling the CSS | file for the … 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

How to change value of a request parameter in laravel

Try to: $requestData = $request->all(); $requestData[‘img’] = $img; Another way to do it: $request->merge([‘img’ => $img]); Thanks to @JoelHinz for this. If you want to add or overwrite nested data: $data[‘some’][‘thing’] = ‘value’; $request->merge($data); If you do not inject Request $request object, you can use the global request() helper or \Request:: facade instead of $request