How To Create Model Object In Controller In Laravel?

Submitted by pakainfo - 3 years ago

Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.

//Laravel how to pass model object to controller via middleware

// app/Providers/RouteServiceProvider.php
public function boot()
{
    parent::boot();
    Route::model('asking', \App\Booking\Booking::class);
}
// routes/web.php
Route::get('/asking/{asking}/lang', 'AskingController@lang')->middleware('asking.validate');
// app/Http/Middleware/ValidateAsking.php
public function handle($request, Closure $next)
{
    $asking = $request->asking;
    // ...
    return $next($request);
}

// app/Http/Controllers/AskingController.php
public function lang(Request $request)
{
    dd($request->asking);
}

//reference a model in a laravel controller?
$car = new \App\Models\CarModel;
//or
use App\Models\CarModel;
....    
class {
    $car = new CarModel;
}


Read: More Visit --- https://www.pakainfo.com/how-to-create-model-object-in-controller-in-laravel/