This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// in file template user | |
<html> | |
<body> | |
@yield('header') | |
@yield('content') | |
@yield('footer') | |
</body> | |
</html> | |
// then in your second file view-one.blade.php | |
@extends('template') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$user=auth()->user(); | |
$post=Post::where('user_id',1)->first(); | |
$allow=\Gate::allows('update-post',$post); // not need to send user it use current logined user | |
// if user not logined allow is false | |
// if you want to user another user for check a gate | |
$allow=\Gate::forUser(User::find(2))->allows('update-post',$post); | |
if($allow){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan make:event RegisterUser // this event actioned | |
// in class EventServiceProvider | |
protected $listen =[ | |
Registered::class=>[ | |
SendEmailVerificationNotification::class, | |
], | |
RegisterUser::class=>[ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$post = new Post(); | |
$post->title = "hello"; | |
$post->des = "des" | |
$post->save(); | |
// if send extra data that is not in table it give you error | |
//--------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if you want to connect in certain database use | |
DB::connection('name_of_connection_in_database.config'); | |
DB::connection('mysql')->getPdo(); // return an object pdo | |
// if you not mention name of connection it by default use env connection name like | |
DB::table('posts'); // in env its set by default mysql (mysqli in this file its just a name 'drive' is importent | |
'connections' => [ | |
'sqlite' => [ | |
'driver' => 'sqlite', | |
'url' => env('DATABASE_URL'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set session---------------------- | |
session()->put('name','morteza');// or | |
session(['name'=>'hamid','age'=>23]); | |
// to get session | |
echo session()->get('name'); //or | |
echo session('name','default'); | |
// check session--------------------- | |
session('name',null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// response should be an object(should be have toString method) or string or array , it can not be a boolean | |
// if you return an array laravel by default convert it to json | |
retrun ["name"=>"ali","age"=>25]; | |
// laravel prefer response | |
return response('hello',201,[ | |
'SAMPLE_HEADER'=>'VALUE' | |
]); | |
return response('hello',201)->header('second_header','salam'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//php artisan down - CheckForMaintenanceMode Middleware in Kernel.php is an example of middleware | |
// middle ware should be define in Kernel.php | |
// it can be effect in whole website or just certain route | |
// you can run middle ware in constructor method of controller | |
// in counstrucor method you can use (only) and (except) method to say meddle ware affect in what method | |
// 2 type of middleware exist 1:befor 2:after |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan route:list // show all list of routes | |
//orginal --------------------------------------- | |
Route::get('/','HomeController@index'); | |
//without method name ---------------------------- | |
// if you not mention name of method so you should use __invoke(): | |
Route::get('/{name}','HomeController'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// belongs to - one to one - one to many - many to mayn | |
class PostModel extends Model | |
{ | |
protected $fillable =['title','des','user_id']; | |
public function user(){ | |
return $this->belongsTo(User::class); | |
// return User::findOrFail($this->user_id); | |
} | |
} |
OlderNewer