Laravel 5.4 Undefined Variable from controller to view - laravel

I recently got started with Laravel 5.4, I'm familiar with 5.3. I'm trying to pass a variable from my controller to the view and keep getting an "Undefined variable $savedStores".
public function index()
{
//
$stores = Store::where('id as user_id')->get();
return view('home')->with('savedStores', $store);
}
That is my controller code.
In my view, I have
#if(count($savedStores)>0)
#foreach($savesStores as $savedStore)
<p>{{$savedStore -> name}}</p>
#endforeach
#endif

return view('home',['savedStores'=>$stores]);
spelling mistake chane $store to $stores

Just change your code:
from:
$stores = Store::where('id as user_id')->get();
to:
$stores = Store::where('id',$some_id_value)->get();
Or you can get all store records by:
$stores = Store::all();
You are passing the variable to view in correct manner.
More details check here:
https://laravel.com/docs/5.4/views

Actually with method used for session data.
You can pass variables to view.
return view('home',['savedStores'=>$stores]);
or
return view('home',compact('stores')); // as store variable.

Try this one,
public function index()
{
$stores = Store::select('*','id as user_id')->get();
dd($stores);//see query result, if you get correct result here, just remove this line
return view('home')->with('savedStores', $stores );
}

Related

How do I Binding Data from Resource data into Blade Laravel?

I Have a Controller Like This
public function print(Request $request)
{
$id = explode(',',$request->id);
$data = DeliveryOrder::whereIn('id', $id)->orderBy('created_at', 'desc')->get();
$dataku = DeliveryOrderResource::collection($data);
// return $dataku;
return view('warehouse.printdo', compact('dataku'));
}
If I return the $dataku,..
and then I Will bind the Data into Blade Laravel, but i get an error
Invalid argument supplied for foreach() (View: ,...resources\views\warehouse\printdo.blade.php)
As you provided the screenshot, you have do_details in first array.
what if you don't have do_details in the second array?
You need to check the condition the foreach you're using have data or not.
#if(!empty($row->do_details))
#foreach($row->do_details as $do => $detOrder)
//additional code
#endforeach
#endif

Redirect from controller to named route with data in laravel

I'm gonna try to explain my problem:
I have a named route called 'form.index' where I show a html form.
In FormController I retrieve all form data.
After do some stuff with these data, I want to redirect to another named route 'form.matches' with some items collection.
URLS
form.index -> websiteexample/form
form.matches -> websiteexample/matches
FormController
public function match(FormularioRequest $request)
{
// Some stuffs
$list = /*Collection*/;
return redirect()->route('form.matches')->with(compact('list'));
}
public function matches()
{
// How to retrieve $list var here?
return view('form.views.matches')->with(compact('list'));
}
The problem:
When the redirects of match function occurs, I get an error "Undefined variable: list' in matches funcion.
public function match(Request $request)
{
// Operations
$list = //Data Collection;
return redirect()->route('form.matches')->with('list',$list);
}
In view
#if(Session::has('list'))
<div>
{!!Session::get('list')!!}
</div>
#endif
You can use Redirect::route() to redirect to a named route and pass an array of parameters as the second argument
Redirect::route('route.name',array('param1' => $param1,'param2' => $param2));
Hope this helps you.

Undefined variable: post (View: E:\xamp\htdocs\lsapp\resources\views\posts\index.blade.php)

I am new to laravel, and getting this error though tried every possible sites and google searches. I even tried previous queries solution from stack overflow, but was unable to solve.
this is my PostController.php:
`public function getData()
{
$post['post']=DB::table('students')->get();
if(count($post)>0){
return view('index',['Post'=>'$post'] );
}
else{
return view('index');
}
}`
this is my controller
This is my index.blade.php:
`#foreach($post as $value)
<tr>
<td>{{$value->id}}</td>
<td>{{$value->name}}</td>
<td>{{$value->address}}</td>
<td>{{$value->number}}</td>
<td><a href="" ><button>Edit</button></a> <a href="" >
<button>Delete</button></a></td>
</tr>`
This is my blade:
Route::get('/index','PostController#getData');
this is my route
please help in this queries, i am having a lots of problem
There are a number of problems with your code. I would recommend you go through laravel documentation and first 2 PSRs
As error clearly states that there is not a variable $post in the view. In else you are not sending the $post variable and in if you are sending the $post variable with a capital P but you are accessing it using lower case p in your view. So the variable is not available in any case in the view that's why it is showing that error.
PHP variables are case sensitive and do not use the quotes around php variable (the one with $ sign) in the parameters that you are passing to the view.
return view('index',['Post'=>'$post'] );
Above statement should be written as
return view('index',['post'=>$post] );
also update your controller method and I don't know why are you using associative array ($post['post']).
public function getData()
{
$post = DB::table('students')->get();
return view('index',['post'=>'$post'] );
}
I identified this problem. You send the posts variable in the index function.
public function index(){
$posts = Post::all();
return view('posts.index',compact('posts'));
}
But most likely you get this error when you want to create a new post, so here the store function is executed and the index function is not executed in the controller, because the store function is responsible for storing form information. Then you edit this function as follows.
public function store(Request $request) {
$post = new Post();
$post->title = $request->input("title");
$post->description = $request->input("description");
$post->save();
$posts = Post::all(); //Send posts in compact
return view("posts.index", compact($posts));
}
Excuse me for the typing problem, I am a Persian speaker ❤

passing different type variables to the view from controller in laravel

i want to pass two variables called as repcounter and suppliers to the view to do two different task.
here is my function in controller,
public function admin()
{
$suppliers = SupplierData::all();
$repcounter= SalesRep::count();
return view('dashboard', compact('suppliers'));
}
this is how i send suppliers data. it worked fine. but i don't have an idea to send repcounter and suppliers at once.. each time i try i gave error undefined variable.
so how to send this two varibles to the dashboard.blade.php view?
You should try this:
public function admin()
{
$suppliers = SupplierData::all();
$repcounter= SalesRep::count();
return view('dashboard', compact('suppliers','repcounter'));
}
Replace:
return view('dashboard', compact('suppliers'));
With the following code:
return view('dashboard', compact('suppliers,'repcounter'));
You can pass multiple variables to the view by nesting the data you want in an array. See below code.
public function admin()
{
//create an empty array
$response = array();
//nest your data insde the array instead of creating variables
$response['suppliers'] = SupplierData::all();
$response['repcounter'] = SalesRep::count();
return view('dashboard', compact('response'));
}
Inside your View, you can access them as below
$response['suppliers']
$response['repcounter']

How to get value of logged in user id in controller in laravel?

Top of my controller i have added:
use Auth;
function in my controller
public function details($id)
{
if(Auth::check())
{
$user = Auth::user();
$product = Product::find($id);
$cart->product_id = $product->id;
$cart->category_id = $product->category_id;
$cart->user_id = $user->id;
dd($cart->user_id); //check if its storing the value
}
else {
return redirect()->route('login');
}
}
when I run this i get error:
Creating default object from empty value
If I remove the $user->id line the error goes.
I tried adding constructor also but still got same error
public function __construct() {
$this->middleware('auth');
}
dd is showing other details after it checks if user is logged in.
Is there a way to get user id of logged in user from users table that is created by default.
Thanks!
The issue is you’re calling $cart->product_id, but the $cart variable isn’t defined as far as I can see. PHP doesn’t know what to do, so because you try and assign a property to it, PHP assumes you want $cart to be a class, hence the “Creating default object from empty value” message.
Other than that, you code could be improved by using middleware to authenticate your users, and relations on your Eloquent models so you’re not manually assigning relation IDs.
Try like this,
You forgot to initials cart object
$cart = new Cart;
$cart->product_id = $product->id;
$cart->category_id = $product->category_id;
$cart->user_id = $user->id;
dd($cart->user_id); //check if its storing the value

Resources