how to pass two variables to view in controller (laravel 5.3) - php

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]);
}

Related

Call to a member function all() on array laravel excel

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);

How to bypass multiple parameter from url to controller

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]);
});
?

Global Value in laravel 5.6

Here is my code ...
public function index()
{
$clientIP = request()->ip();
$cats=DB::table('catagory')->get();
$news=DB::table('news')->orderby('nID','desc')->limit('5')->get();
$books=DB::table('book')->where('bPrice','=',0)->orderby('bID','desc')->limit('5')->get();
return view('index',compact('clientIP','cats','news','books'));
}
As you see there are some variables, I passed them to my view but I have to repeat them in every view I wanna show.Is there any way to globalize them and use anywhere.Please help with sample code.Thanks.
Laravel view composer may help you.
You should add this code to your app/Providers/AppServiceProvider.php.
view()->composer(['index', 'index1', 'index2'], function($view) {
$clientIP = request()->ip();
$cats=DB::table('catagory')->get();
$news=DB::table('news')->orderby('nID','desc')->limit('5')->get();
$books=DB::table('book')->where('bPrice','=',0)->orderby('bID','desc')->limit('5')->get();
$view->with([
'clientIP' => $clientIP,
'cats' => $cats,
'news' => $news,
'books' => $books,
]);
});

i Can't pass three arrays to laravel view

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

Yii2: Call to undefined method backend\models\CreateBookings::model()

Trying to fetch data from db
in controllers
$model = new CreateBookings();
$data = CreateBookings::model()->findByPk();
return $this->render('view', [
'data' => $data,
]);
in view :
<?php echo $data->booking_id?>
can somebody tell me what am i doing wrong?
The alternative for Yii1 findByPk in Yii2 is findOne($id)
$model = CreateBookings::findOne($id);
or the equivalent
$model = CreateBookings::find()
->where(['id' => $id])
->one();
for your view you can use
$data = CreateBookings::findOne($id)) ;
and you should have the $id value.
Answering My own question:
I wanted to show database values in my View.
In controller actionView I added-
$data = CreateBookings::findOne($id);
and rendered
'data' => $data,
To show value in view I used
<?php echo Html::encode($data->booking_id);?>

Categories