how to display search result in laravel - laravel

i can not show search result using laravel. below is the code for route, controller and views:
route:
Route::resource('search', 'SearchController');
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order;
use Redirect;
use Auth;
use DB;
class SearchController extends Controller
{
public function index(Request $request)
{
$order = Order::all();
return view(search.index', compact(search));
}
public function search(Request $request)
{
$search = $request->search;
$order = DB::table('order')
->where('custName','like',"%".$search."%");
return view('search.index',compact('search'));
}
}
view
<form action="search" method="GET">
<input type="text" name="custName" class="form-control" <br>
<button type="submit" class="btn btn-primary">SEARCH</button>
</div>
</div>
<table class="table table-bordered" width="500">
<tr>
<td><font color="white">RESULT :</font></td>
</tr>
#foreach($order as $order)
<tr>
<td>{{ $order->custName }}</td>
<td>{{ $order->orderStatus }}</td>
</tr>
#endforeach
</table>
directory location
c:/xampp/htdocs/web/resources/views/search/index.blade.php
show error
Undefined variable: order (View: c:\xampp\htdocs\web\resources\views\search\index.blade.php)
above is my code to search record in the database. but the result always undefined variable.
how to display the searched result using code above. thank you

In your Controller search method, assign search result to $orders
$orders = DB::table('order')
->where('custName','like',"%".$search."%");
then send it to the view:
return view('search.index',compact('orders'));
and in the view do this:
#foreach($orders as $order)
<tr>
<td>{{ $order->custName }}</td>
<td>{{ $order->orderStatus }}</td>
</tr>
#endforeach
It will solve your problem.

There are some issues with the code you have shared, I try to tell the ones I see.
there is a misspelling in Route::resource where instead of search you coded seearch
as for search method it is not used anywhere by the route you provided. You can see more details on Route::resource for methods and views at this question
now considering that you are loading index.blade.php from the public function index method, the usage of compact is troubled. At this page, you can see how compact and with are different and how to use it. The main problem here is that you are not actually passing the order to your view at all.
The last but not least, it is good practice to name items having a collection of values plurally. For example here the order should change to orders. This would prevent the misuse of it in foreach. In your foreach, the name of the iterating array and the temp variable keeping the data should be different, or otherwise, they would have conflict. Here you have 2 variables called order in the same scope.

for instance i have records :
a
aa
aaa
b
c
d
e
the code show all record no1 until no7.
but now its solved i fixed the code in my controller in index() section as below
public function index()
{
return view('search.index', compact('order'));
}
public function search(Request $request)
{
$search = $request->search;
$order = DB::table('order')->where('custName', 'like', "%".$search."%")->get();
return view('search.result', compact('order'));
}

Related

How to get the list of client related through the HasManyThrough relationship

I have these 3 tables:
clients
id - integer
name - string
projects
id - integer
client_id - integer
name - string
tasks
id - integer
project_id - integer
name - string
related through the HasManyThrough relationship.
In the client table I have a column relating to the number of tasks relating to the projects of that specific client.
I want that when I click on the number of tasks, I am redirected to the view of the client related tasks. While when I click it shows me all the tasks, but I want, as already specified, the tasks related to the projects of that specific client.
How can I solve this problem?
VIEW:
<tr>
<th class="id">ID</th>
<th class="">Name</th>
<th class="">Task</th>
</tr>
#foreach ($clients as $client)
<tr>
<td>{{ $client->id }}</td>
<td>{{ $client->name }}</td>
<td>
<a href="{{route('task.index'}" class="link-primary">{{
$client->tasks_count }}
</a>
</td>
#endforeach
TaskController:
public function index()
{
$tasks = Task::all();
//dd($tasks);
return view('task.index', compact('tasks'));
}
Create new route:
Route::get('/client/{client}/tasks', ['as' => 'client.tasks', 'uses' => 'ClientController#tasks'])->name('client.tasks');
Add something like this to your view:
//Not sure if that is a right syntax to bind the id in the route, need to verify
<a href="{{ route('client.tasks', [$client->id]) }}" class="link-primary">
{{ $client->tasks_count }}
</a>
Or:
//Route
Route::get('/client/{client}/tasks', [ClientController::class, 'tasks'])->name('client.tasks');
//and view
<a href="{{ URL::to('/client/' . $client->id . '/tasks') }}" class="link-primary">
{{ $client->tasks_count }}
</a>
And controller:
public function tasks(Client $client)
{
$tasks = $client->tasks;
//return your view with tasks associated with this client
}
As you have written
$tasks = Task::all();
All tasks!
You need to pass along the id of the client with the request, preferably with route model binding so that you get a client instance in the controller. Then you can load the tasks or the projects with tasks below them
public function index(Client $client)
{
$tasks = $client->tasks()->get();
//dd($tasks);
return view('task.index', compact('tasks'));
}
or with projects
{
$projects = $client->projects()->with('tasks')->get();
return view('task.index', compact('projects'));
}
In this second example, you get a collection of projects with the tasks inside, so you can then show a list of projects and a list of tasks for that project

linking in the laravel join function

I get data from two different tables with Join function. But the link code for the edit page does not work.
index.blade.php file
#foreach($data as $row)
tr>
<td>{{$row->customer_name}}</td>
<td>{{$row->customer_phone}}</td>
<td>{{$row->product_name}}</td>
<td>{{$row->sale_date}}</td>
<td>{{$row->delivery_date}}</td>
<td>
<a href="{{route('orders.create'),[$row['order_id']]}}">
<button class="btn btn-info btn-sm fa fa-edit"></button></a>
</td>
</tr>
#endforeach
web.php file
Route::group(['namespace'=>'orders', 'prefix'=>'orders', 'as'=>'orders.'], function (){
Route::get('/', 'indexController#index')->name('index');
Route::get('/create', 'indexController#create')->name('create');
Route::post('/create/', 'indexController#store')->name('create.post');
Route::get('/detail/{id}', 'indexController#show')->name('detail');
Route::get('/edit/{id}', 'indexController#edit')->name('edit');
Route::post('/edit/{id}', 'indexController#update')->name('edit.post');
});
indexController.php file
public function index()
{
$data = DB::table('orders')
->select('orders.customer_name', 'orders.customer_phone', 'products.product_name', 'orders.sale_date','orders.delivery_date')
->join('products', 'orders.product_id', '=', 'products.product_id')
->get();
return view('orders.index', compact('data'));
}
ErrorException (E_ERROR)
Cannot use object of type stdClass as array (View: D:\xampp\htdocs\personality\resources\views\orders\index.blade.php)
You have an stdClass object, you can't get parameters from it using array syntax.
$object->parameter; // object
$array['element']; // array
So instead of this:
$row['order_id']
Can you try this:
$row->order_id
Pass the ID of a model as a second argument to route and access the attribute of a model (object) with the arrow operator
<a href="{{ route('orders.create', $row->order_id) }}">
Hope this helps

Invalid argument supplied for foreach() on laravel

I have like error message : Invalid argument supplied for foreach()
For my method index() I have this:
public function index(Request $request)
{
if(\Auth::user()->hasRole('admin')){
if($request->has('search'))
$remarks = Remark::orderBy('instruction', 'asc')->where('instruction','like','%'.$request->input('search').'%');
else
$remarks = Remark::all();
}else{
\Auth::user()->load('remarks');
$remarks = \Auth::user()->remarks;
}
return view('admin.remarks.index', compact('remarks'));
}
Concerning my index.blade.php I have my loop "for" which is like this:
<th>Instruction</th>
<th>Description</th>
<th>Date seance</th>
<th>Name Candidate</th>
<th>Email</th>
</tr>
</thead>
#foreach($remarks as $remark)
<tr>
<td> {{$remark->instruction}}</td>
<td> {{$remark->description}}</td>
<td> {{$remark->seances->date_seance}}</td>
<td> {{$remark->seances()->first()->candidates->name}}</td>
<td> {{$remark->seances()->first()->candidates->email}}</td>
<td>
Do you have an idea of my problem please? I don't understand ??
You need to call get() after the Eloquent Query Builder to fetch the data from the database. Unless it's an Eloquent Query Builder Object (which is not iterable, so cannot be used in a for loop).
The function get() will return the result as a Laravel Collection (which is iterable).
$remarks = Remark::orderBy('instruction', 'asc')
->where('instruction','like','%'.$request->input('search').'%')
->get();
you should querying from database not from session and don't forgot define relations on users model :
on users model:
public function remarks(){
return $this->hasMany(Remark:class)
}
on your controller
public function index(Request $request)
{
if(\Auth::user()->hasRole('admin')){
if($request->has('search'))
$remarks = Remark::orderBy('instruction', 'asc')->where('instruction','like','%'.$request->input('search').'%');
else
$remarks = Remark::all();
}else{
$user = Users::find(Auth::id());
$remarks=$user->remarks
}
return view('admin.remarks.index', compact('remarks'));
}

Get related objects on the view

I have related model
class Myevent extends Model
{
public function photo()
{
return $this->hasMany('App\EventPhoto');
}
}
Ob blade if loop i get items Myevents
#foreach($evetns as $event)
<b>{{$event->name}}</b> <br>
{{$event->place}} <br>
{{$event->description}} <br>
#foreach($event->photo() as $item)
{{$item->id}}
#endforeach
#endforeach
How can I call related objects in a loop on blade?
You seem to be doing correctly, except for one single error $event->photo instead of $event->photo()
#foreach($event->photo as $item)
{{$item->id}}
#endforeach
To clear the confusion regarding brackets https://laraveldaily.com/calling-eloquent-from-blade-6-tips-for-performance/

get the Id from a item grid and save into a variable

I'm working with Laravel 5 to do the following: I have a grid that shows all the clients that the company has, in each row of that grid, there is a button to select that client like this:
Grid of clients
This button sends me to a separate screen that is related directly with the selected client, when this happens, the URL is as follows:
http://sistemaprueba.com/modulos?1
where the value after the question mark represents the value of the id of the selected client. Now, I need to save the value of that id in a variable to be able to use it later, how can I do that?
This is my grid:
<div class="row">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Nombre Cliente</th>
<th>Ingresar</th>
</tr>
</thead>
<tbody>
#foreach($results as $result)
<tr>
<td>{{$result->nombre_cliente}}</td>
<td></i>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
This is the route to the new screem:
Route::get('/modulos', 'HomeController#modulos')->name('modulos');
and my controller looks like this:
class ClientController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$id = \Auth::user()->id;
$results = DB::table('clients')->join('clients_company', 'clients_company.fk_id_client', '=', 'client.id')
->where('clients_company.fk_id_usuar',$id)->get();
return view('clients',compact('results'));
}
public function modulos()
{
return view('modulos');
}
}
You could either change your href to "/modulos/1" and your route and controller to:
// route
Route::get('/modulos/{clientId}', 'HomeController#modulos')->name('modulos');
// controller
public function index($clientId)
{
// Use $clientId
}
Or give a named parameter in your url, like "/modulos?clientId=1", then:
// route (unchanged)
Route::get('/modulos', 'HomeController#modulos')->name('modulos');
// controller
public function index()
{
$clientId = request()->input('clientId');
...
}
Alternatively, you could parse the url with PHP's function parse_url() in your controller, to extract the GET part of the url for you to manipulate. Not the cleanest solution. http://php.net/manual/en/function.parse-url.php
The first method may be preferable for your purpose.

Resources