How do I Binding Data from Resource data into Blade Laravel? - 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

Related

Too few arguments to function App\Http\Controllers\SupervisorController::edit(), 0 passed and exactly 1 expected

Sorry this is very basic, but i dont know what is the problem.
I always get this error when trying to visit edit_supervisor.
Route:
Route::get('/edit_supervisor', 'SupervisorController#edit')->name('edit_supervisor');
SupervisorController:
public function edit($id)
{
return view('DataSupervisor.edit');
}
you can add $id as a parameter like this in Route
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
or you can change the function parameter $id to Request $request
public function edit(Request $request)
{
return view('DataSupervisor.edit');
}
in blade
edit
for showing previous data you need to get data using $id in controller
$supervisor = Model::findOrFail($id);
return view('DataSupervisor.edit', compact('supervisor'));
You need to pass the id :
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
On blade :
<a href="{{ route('edit_supervisor', $id) }}...

Trying to get property 'post_titile' of non-object

This is My Controller.
public function show(Posts $posts)
{
$page = Posts::find($posts->id);
//dd($page);
return view('web_views.index',['page' => $page]);
}
This is my view page
<h4>{{$page->post_titile}}</h4>
It's better to use Route-Model Binding I think.
Your route (if you are using resource route, it's already done):
Route::get('posts/{post}', 'PostController#show); // domain.tld/posts/1
Your method should look like this:
public function show(Post $post)
{
return view('web_views.index',compact('post'));
}
In your view:
<h4>{{ $post->post_titile }}</h4>
May be $posts->id you are passing, has no result in database so you need to check it by using if-statement
public function show(Posts $posts)
{
$page = Posts::find($posts->id);
if($page == null){
$page = ['post_title' => 'Not Available'];
}
return view('web_views.index',['page' => $page]);
}
I believe this error is because of not finding data for an ID into database. So if as per above script will pass the fetched data to view otherwise it will push an item into $page array.

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 ❤

how to use $request->all() on controller and blade view in laravel 5.5

in Controller
public function index(Request $request)
{
$search_cons = $request->all();
$search_con = $search_cons->name; //error place
return $search_cons.$search_con;
}
->name this place has the error
Trying to get property of non-object
Or in blade.view
<p>{{$search_cons->name}}</p> has the error
Trying to get property of non-object
But if I use
$search_cons=$request->input('name');
on controller
the blade view
<p>{{$search_cons}}</p> will work ok!
I want the $search=request->all() so I can freely use $search->name on my blade view
How can I fix the question?
PS: I tried $resquest('name') still not to work
Request::all() ->tell me the
When you do $request->all() it returns all the inputs submitted in array format. So in your case, you can do
$search_cons = $request->all(); // dd($search_cons) so you can see its structure
$search_con = $search_cons['name']; // instead of ->name since it's not an object anymore
And anyway, you can skip the $request->all() thing - you can actually just do this directly:
$request->name.
EDIT:
You can cast the array as an object using (object)
$search_cons = (object) $request->all();
this will still let you use $search_cons->name
$search_cons is an array, not an object:
$search_con = $search_cons['name']
public function index(Request $request)
{
$search_cons = $request->all(); //returns array
$search_con = $search_cons['name']; //error place
return $search_cons.$search_con;
}
Or you can do like this
request()->name //request() isa global helper function
Try
In Controller
public function index(Request $request)
{
$search = $request->all();
return ['search' => $search];
}
In Blade
<p>Name : {{$search['name'] ? $search['name'] : ''}} </p>

Laravel 5.4 Undefined Variable from controller to view

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

Resources