laravel migration best way to add foreign key

Firstly you have to make your user_id field an index: $table->index(‘user_id’); After that you can create a foreign key with an action on cascade: $table->foreign(‘user_id’)->references(‘id’)->on(‘users’)->onDelete(‘cascade’); If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch. On down() function you have … Read more

Laravel: Validate maximum File size?

According to the documentation: $validator = Validator::make($request->all(), [ ‘file’ => ‘max:500000’, ]); The value is in kilobytes, for example: max:10240 = max 10 MB. max:1 = max 1024 bytes. Note that there are efforts to change the value of 1 kilobytes from original 1024 to 1000 bytes, but major frameworks like Laravel remain using original … Read more

Clone an Eloquent object including all relationships?

tested in laravel 4.2 for belongsToMany relationships if you’re in the model: //copy attributes $new = $this->replicate(); //save model before you recreate relations (so it has an id) $new->push(); //reset relations on EXISTING MODEL (this way you can control which ones will be loaded $this->relations = []; //load relations on EXISTING MODEL $this->load(‘relation1′,’relation2’); //re-sync everything … Read more

Laravel Eloquent update just if changes have been made

You’re already doing it! save() will check if something in the model has changed. If it hasn’t it won’t run a db query. Here’s the relevant part of code in Illuminate\Database\Eloquent\Model@performUpdate: protected function performUpdate(Builder $query, array $options = []) { $dirty = $this->getDirty(); if (count($dirty) > 0) { // runs update query } return true; … Read more

Modify existing Authorization module (email to username)

Laravel search the variable $username in the file : Illuminate\Foundation\Auth\AuthenticatesUsers public function loginUsername() { return property_exists($this, ‘username’) ? $this->username : ’email’; } As you can see, by default it will be named as ’email’. However you can override it in your AuthController by adding : protected $username=”username”;

Laravel – search with multiple keywords against multiple columns with the search result to be ordered in relevance

I’m not sure whether this has another method. But this is what I thought of it $word1 = ‘word1’; $word2 = ‘word2’; $word3 = ‘word3’; $all = DB::table(‘posts’) ->where(‘meta_name’, ‘like’, “%{$word1}%”) ->where(‘meta_name’, ‘like’, “%{$word2}%”) ->where(‘meta_name’, ‘like’, “%{$word3}%”) ->orWhere(function($query) use ($word1, $word2, $word3) { $query->where(‘meta_description’, ‘like’, “%{$word1}%”) ->where(‘meta_description’, ‘like’, “%{$word2}%”) ->where(‘meta_description’, ‘like’, “%{$word3}%”); }); $twoWords = … Read more

How to make public folder as root in Laravel?

Do not modify any Laravel files. Instead configure your web server (Apache or Nginx) to point to the Laravel project’s public directory. For Apache you can use these directives: DocumentRoot “/path_to_laravel_project/public” <Directory “/path_to_laravel_project/public”> For nginx, you should change this line: root /path_to_laravel_project/public; Having your document root as /path_to_laravel_project/ alone will create serious security risks, potentially … Read more