I tried to pass three arrays to a view in laravel
but I got this problem
Undefined variable: demmande (View: C:\wamp\www\project\resources\views\demmande\demmandes.blade.php)
i change the order of the arrays i can't pass the third array
here is my function in the controller
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = array('villes' => $listvillee, );
$demmande = array('demmande' => $listdemmande, );
$categorie = array('categorie' => $listcategorie, );
return view("demmande.demmandes",$villes,$categorie,$demmande);
}
You can use compact() method.
Try this line to return your data.
return view("demmande.demmandes",compact('villes','categorie','demmande'));
Just replace your code with this,
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = $listvillee;
$demmande = $listdemmande;
$categorie = $listcategorie;
return view("demmande.demmandes",compact('villes','categorie','demmande'));
}
And you can retrieve those variables by
#foreach ($demmande as $data)
{{$data->property}} //your property to define
#endforeach
Hope this will work.
From reading the Laravel Views documentation I think that the view() method expects you to specify the template parameters using one array. You can combine your three arrays into one:
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$data = array(
'villes' => $listvillee,
'demmande' => $listdemmande,
'categorie' => $listcategorie,
);
return view("demmande.demmandes", $data);
}
According to the official documentation you can either pass an array as second parameter, as opposed to the list of all the parameters.
return view("demmande.demmandes", [
'villes' => $villes,
'categorie' => $categorie,
'demande' => $demande
]);
or chain the with method to add more parameters (see the Github page).
return view("demmande.demmandes")
->with('villes', $villes)
->with('categorie', $categorie)
->with('demande', $demande);
The view function will only accept a single array, however, you can nest your arrays within it like this -- and still access them by key from within the view.
return view("demmande.demmandes",['villes'=>$villes, 'categorie'=>$categorie, 'demmande'=>$demmande]);
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = array('villes' => $listvillee, );
$demmande = array('demmande' => $listdemmande, );
$categorie = array('categorie' => $listcategorie, );
return view("demmande.demmandes",compact('villes','categorie','demmande');
A simple example
View the controller in app/Http/Controllers/SampleController.php
/**
* #return View
*/
public function index()
{
$movieList = [
'Shawshank redemption',
'Forrest Gump',
'The Matrix',
'Pirates of the Carribean',
'Back to the future',
];
return view('welcome', compact('movieList'));
}
and for example, See latest movie views in resources/views/welcome.blade.php
#section('content')
<h1>Latest Movies</h1>
<ul>
#foreach($movieList as $movie)
<li class="list-group-item"><h5>{{ $movie }}</h5></li>
#endforeach
</ul>
#endsection
Related
i'm using laravel maatwebsite excel,
i tried passing variable and do some action and return as array to export,
So i get the Error when i tried to pass array to the main controller for excel export(download)
my main controller
public function excel_export(Request $request){
return Excel::download(new UsersExport($request->exp_vlas), 'users.xlsx');
}
here iam passing variable to the collection
my export controller
public function collection(){
$instruments = implode(",",$this->id);
$instruments = explode(",",$instruments);
//$i=0;
foreach ($instruments as $instrument) {
$instr_list = DB::table('instruments')->select('*')->where('id',$instrument)->get()->toArray();
$arr_instrulist[] = $instr_list;
$instrument_var[] = $instrument;
$instr_list = "";
//$i++;
}
$arr_instrulist_excel[] = array('Instrument Name', 'Serial', 'Qa id', 'Unit', 'Range');
foreach($arr_instrulist as $arr_instrulists){
//$arr_instrulists = array($arr_instrulists);
$arr_instrulist_excel[] = array(
'Instrument Name' => $arr_instrulists[0]->instrument_name,
'Serial' => $arr_instrulists[0]->serial,
'Qa id' => $arr_instrulists[0]->qa_identification_no,
'Unit' => $arr_instrulists[0]->unit,
'Range' => $arr_instrulists[0]->range
);
}
return $arr_instrulist_excel;
}
when tried to return this($arr_instrulist_excel) i get an error
please give me some solution for this.
Error i'm facing
You are telling export script to get your data from Collection but you are giving an array. You should return collection instead.
You can simply wrap your array in collection like that:
return collect($arr_instrulist_excel);
I have 2 parameters that I put in my url. What I want is how to get these 2 parameters, so it can be read from my controller.
I've try this so the controller can read the parameter:
$id = Input::get('id');
but when I check with dd, the parameter is null
Here is my code:
The link a href on view:
{{ URL('/lap_spd/tambahspd/'.$items->nosurat.'/'.$items->id )}}
web php :
Route::get('lap_spd/tambahspd/{id}/{nosurat}', function($id, $nosurat){
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id], ['nosurat' => $nosurat]);
});
lapspdController php:
public function tambahuwong($id, $nosurat) {
$id = Input::get('id');
$nosurat = Input::get('nosurat');
$data3 = DB::table('list_nama')
->where('id_nosurat', 'nosurat')
->toSql();
dd($data3);
so the output that I want - is the parameter can pass to variable on controller.
Thanks for respone before and sorry for my bad english.
Pass the parameters in a single array in your web.php file
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id, 'nosurat' => $nosurat]);
In lapspdController php:
public function tambahuwong($id, $nosurat) {
//remove these lines and use the variables from the parameters directly
//$id = Input::get('id');
//$nosurat = Input::get('nosurat');
$data3 = DB::table('list_nama')
->where('id_nosurat',$nosurat)
->toSql();
dd($data3);
in view:
in route:
Route::get('lap_spd/tambahspd/{id}/{nosurat}','HomeController#tambahuwong')
->name('check');
in controller:
public function tambahuwong($id,$nosurat) {
$check_id=$id;
$nosurat_name=$nosurat;
dd($check_id);
dd($nosurat_name);
}
Does it work if you change
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id], ['nosurat' => $nosurat]);
});
to
return redirect()->action(
'lapspdController#tambahuwong', ['id' => $id, 'nosurat' => $nosurat]);
});
?
I have two different queries which are saved in two variables.I want to pass the variables to view page from controller.
public function getApprovalList(){
// $users = select query..
// $request = select query..
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
solution:
controller
return view('travelerHome',['users'=>$users,'requestList'=>$request]);
view
#foreach ($requestList as $req)
{{$req->traveler_name }}
#endforeach
return view('travelerHome')->with(array('users'=>$users,'request'=>$request));
You can use return view('travelerHome', compact('users', 'request')); too
Do like this
return view('travelerHome')->with('users'=>$users)->with('request'=>$request);
Code with you have written that is correct, you need to remove the comment only, I am also using the same method for pass the variable to View. Here is the example.
return view('admin.product.product.edit', ['product' => $product,
'attribute_set' => $productAttributes,
'category' => $cates,
'images' => $images,
'status' => $status,
'countries' => $countries,
'crane_manufacture' => $crane_manufacture,
'product_category' => $productCategory]);
}
And it's also working
So, in your code you need to
public function getApprovalList(){
$users = 'select query..';
$request = 'select query..';
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
I have the following method in a controller
public function artikel(){
$breadcrumb = array(
'Home' => URL::to('/'),
'Artikel' => ''
);
$this->layout->title = "Egoji";
$artikels = Posting::orderBy("created_at","desc")->where("postings.tipe","=","artikel")->paginate(5);
$this->layout->content = View::make("frontend.artikel.index",array("artikels"=>$artikels, 'breadcrumb'=>$breadcrumb))->render();
}
in the view frontend.artikel.index i am trying to takeout first artikel using $artikels->shift(). i get the first artikel, but it doesnt remove from the collection, it still there when i loop the rest.
I have this:
Route::get('/product/{id}', 'PagesController#display');
And in my PagesController I have this:
public function display($id) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}
How can I pass the variable $premium to the controller method without inserting it in the url? In simple words I don't want this: www.mysite.com/product/false/125 (false is the value of $premium) but this: www.mysite.com/product/125. That's why I have just $id as parameter in the controller method and no also $premium. I want to pass that variable $premium in other way.
I've try some approaches like this one:
Route::get('/product/{id}', 'PagesController#display')->where("premium" => "false");
which didn't work.
You can use a closure route and call the action "manually":
Route::get('/product/{id}', function($id){
return app('PagesController')->callAction('display', [$id, false]);
});
And
public function display($id, $premium) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}