Skip to main content

Posts

laravel snappy pdf

Install this: https://wkhtmltopdf.org/downloads.html then install this two, $ composer require h4cc/wkhtmltopdf-i386 0.12.x $ composer require h4cc/wkhtmltoimage-i386 0.12.x Then, composer require barryvdh/laravel-snappy Then, php artisan vendor:publish --provider= " Barryvdh\Snappy\ServiceProvider " And Now, copy the wkhtmltopdf.exe and wkhtmltoimage.exe files to the above h4cc folder which is present inside vendor folder of laravel. and set the path to snappy.php file-> see the snappy.php file which is given below: [NOTE: see the code which i've commented out. You can run & check every line of code] Route :: get ( '/' ,  function  () {      $data  = [         [              'name' => 'Audi' ,              'logo' => 'audi.jpg' ...

Eloquent Relationship Explained (MANY TO MANY) with attach,detach,sync

When you create a brand new project you have users table and User model already by default. So, now create another table with model: php artisan make:model Role -m This will create Role model as well as roles table. Now as it is many to many relation. So we need a pivot table. Pivot table naming convention: role_user Here role is written first as because alphabetically r comes before u. and laravel eloquent relationship takes in this way. So, you should remember this always. and use _ in between role and user. like: role_user So, let's create role_user table. php artisan make:migration create_role_user_table --create=role_user naming covention of pivot says that role_user table is singular. users table includes: id name, email pivot table role_user includes: user_id role_id roles table includes: id name migrate it all. Many to Many Relation: Go to User model and write: public function roles(){  r...

Eloquent Relationship Explained (ONE TO MANY)

Suppose you have two tables: users and posts tables. users table includes: id ,name,email posts table includes: id,title,body, user_id One to many relationship: Go to User model and write: public function posts(){  return $this->hasMany('App\Post'); } Look at the function naming convention. It's plural as it is one to many relationship. Store: $user->posts()->save($post); Here, $user variable comes from findorfail($id). and $post variable has taken the requested data from user. I haven't shown all of this in details here as it is only to understand how one to many relationship actually works. Show: foreach($user->posts as $post){    echo $post->title."<br>"; } Update: $user->posts()->where('id','=',2)->update(['title'=>'your updated title','body'=>'your updated body'); Delete: $user->posts->whereId(1)->delete(); To delete all t...

Eloquent Relationship Explained (ONE TO ONE)

One to one relationship: Suppose you have two tables users and addresses: Users table: id ,name,email Addresses table: id,name, user_id In the address table the user_id will come from the users table. Be careful about the naming convention. Your Model name is User so table name is users so to get id from users table to addresses table you have to write according to the model name in the field of address like user_id . So to declare one-to-one relationship: Just think that user can have only one address. So,  go to User model and write: public function address(){  return $this->hasOne('App\Address'); } here as this is one to one we are using singular function name which is address(). Store: $user->address()-> save($address); [Here, $user is finding the user by id, and $address is saving the requested address] Show: To show the address name $user->address->name; Update: $address = Address::whereUserId(1)->first(); $address...

Add [name] to fillable property to allow mass assignment on [App\Customer].

This mass assignment error occurs when you don't declare protected or guarded to your model. so, just write protected $fillable=[              'name', ]; or, protected $guarded=[]; both are same. $guarded is the opposite of $fillable but acts the same. but if you put name inside $guarded like: $guarded =['name'], than it will only guard this means it will not allow you to give data to name. so, living guarded blank is same as protected $fillable =['name'];