ads

laravel pass value to include


laravel pass value to include

 Pass Data to Include Laravel

This article will show you how to pass data to view in laravel framework. Here I am not just showing only pass data view include I am trying to cover every way that how to pass data to view. It will help you in the future how you can pass data views in multiple ways. 

Let's start with an example as If you are on the home page and you exclude the master layout on this home page. In the Home page, you have a search form and search form has to GET methods to pass a parameter into the page and show the result into the current page.

It means if you are using the same search form into multiple pages with a different link than you can pass dynamic variables into the form of search. suppose you can take Home, About us, and Gallery page they same inherited the same search form with include so into form action you can pass dynamic URL variable with include of laravel.

Step:1 create form page in laravel resources/views/searchForm.blade.php
<form class="seach-form" action="{{url('/')}}" method="GET">
{{csrf_token()}}
<div class="form-group">
<label>Seach Keywork</label>
<input class="form-control" type="text" placeholder="Seach Keyword">
</div>
</form>

Step:2 go to your home page include this form to your home with formUrl variable 
@include('pages.searchForm',['formUrl' => "request()->url()"])


Step:3 This variable you can pass in form action different blade form like below. replace this below line to step 1
<form class="search-form" action="{{$formUrl}}" method="GET">

so if you are in the home page then you can pass the home page current URL, if you are in the about us page then it will auto pass about current URL into formUrl variable it will take dynamic value into a different page via this include variable which is passed here above into include of view.

Second Way use "Compact"

Step:1 use into controller function which is redirecting to your particular page
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontendController extends Controller
{
public function home()
{
$formUrl = \request()->url();
return view('home', compact('formUrl'));
}
}

Step:2 you can use like below home blade file
<form class="search-form" action="{{$formUrl}}" method="GET"></form>

Third Way use "With"

Step:1 use into controller function which is redirecting to your particular page

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontendController extends Controller
{
// For admin application
public function home()
{
$formUrl = \request()->url();
return view('home')->with('formUrl',$formUrl);
}
}

Step:2 you can use like below home blade file
<form class="search-form" action="{{$formUrl}}" method="GET"></form>

*******************************SUCCESS MSG USING WITH**************************

Step:1 controller store method who storing the data and throw the success message as I give an example here
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontendController extends Controller
{
// For admin application
public function store(Request $request)
{
$validated = $request->validate([
"name" => "required",
"status" => "required"
]);
Category::create($request->all());
return redirect()->back()->with("success","Record created successfully.");
}
}
Step:2 frontend where you can fetch the success message using blade file 

@if(session()->has('success'))
<div class="alert alert-success">
<h5>{{session()->get('success')}}</h5>
</div>
@endif


*******************************ERROR MSG USING WITH**************************

Step:1 controller store method who storing the data and throw the success message as I give an example here

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontendController extends Controller
{
// For admin application
public function store(Request $request)
{
try{
$validated = $request->validate([
"name" => "required",
"status" => "required"
]);
Category::create($request->all());
return redirect()->back()->with("success",'Record added successfully.');

}catch (\Exception $e){
return redirect()->back()->withErrors(['Something went wrong']);
}
}
}

Step:2 display that errors in the frontend side

@if(session()->has('errors'))
<div class="alert alert-danger">
<h5>{{session()->get('errors')}}</h5>
</div>
@endif

Fourth Way use "view()->share()"

Step:1 use into controller function which is redirecting to your particular page

public function index(){
view()->share("categories",Category::all());
return view('home');
}

Step:2 you can use like below home blade file

<ul>
@foreach($categories as $key => $value)
<li>{{$value->name}}</li>
@endforeach
</ul>

That's it you can use it and try it into your laravel project.

Thank you.
Happy Coding :)

Post a Comment

أحدث أقدم