I have this function in one of my controllers:
public function getApplicationFiles(Contract $contract) {
dd($contract->get());
}
The parameter is defind in the url. So my route is
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles');
My problem is that the function getApplicationFiles displays all entries from the Contract type and not only the Object with the given id?
What am I doing wrong?
I would go with the following, make your route a named route like this:
Route::get('contracts/files/{contract}', 'ContractController#getApplicationFiles')->name('contract.files');
Then in your view use it like this:
Files
This should give you the expected result.
u don't need to pass all row u need to pass only one row try like this(if you are paassing only contract id in request)
public function getApplicationFiles(Contract $contract) {
//dd($contract);
return response()->json($contract); // if you pass json
return view('show',compact($contract); /// if you view
}
Related
guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();
I have in my View:
Edit</td>
But when I put 1-50 in my $property->user_id parameter, it leads to a property.
I have Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit'); in my web.php file.
Route File:
Route::get('/properties/', 'PropertyController#index');
Route::get('/properties/{property}', 'PropertyController#show');
Route::get('/agents/{agent}', 'AgentController#index');
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
Route::post('/agents/{agent}', 'AgentController#update')->name('agent.property.update');
This is my Controller code:
public function edit($id)
{
$property = Property::find($id);
return view('agents.edit', compact('property'));
}
I don't understand this behavior in Laravel, it's not what I intend and I just want to make the route work correctly.
Laravel gives both agent_id and property_id to the controller as parameter. You are using agent_id only and assuming it as property id.
public function edit($agent_id, $property_id)
{
$property = Property::find($property_id);
return view('agents.edit', compact('property'));
}
Based on your route file, I think you should put this route:
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
above this one:
Route::get('/agents/{agent}', 'AgentController#index');
I am trying to make my URL more SEO friendly on my Laravel application by replacing the ID number of a certain object by the name on the URL when going to that specific register show page. Anyone knows how?
This is what I got so far and it displays, as normal, the id as the last parameter of the URL:
web.php
Route::get('/job/show/{id}', ['as'=>'website.job.show','uses'=>'HomeController#show']);
Controller method
public function show($id){
$job = Job::findOrFail($id);
return view('website.job')->with(compact('job'));
}
Blade page where there is the link to that page
{{$job->name}}
You can overwrite the key name of your Job model:
public function getRouteKeyName()
{
return 'name';
}
Then in your route simply use {job}:
Route::get('/job/show/{job}', ...);
And to call your route:
route('website.job.show', $job);
So your a tag would look like this:
{{ $job->name }}
Inside your controller, you can change the method's signature to receive the Job automatically:
public function show(Job $job)
{
return view('website.job')
->with(compact('job'));
}
For more information, look at customizing the key name under implicit binding: https://laravel.com/docs/5.8/routing#implicit-binding
You need simply to replace the id by the name :
Route::get('/job/show/{name}', ['as'=>'website.job.show','uses'=>'HomeController#show']);
In the controller action:
public function show($name){
//Make sure to replace the 'name' string with the column name in your DB
$job = Job::where('name', $name)->first();
return view('website.job')->with(compact('job'));
}
Finally in the blade page :
{{$job->name}}
2 options:
1) one is like #zakaria-acharki wrote in his comment, by the name of the job and search by the name for fetching the data
2) the second is to do it like here in stackoverflow
to build the url with the id/name
in this way you will make sure to fetch and show the relevant job object by the unique ID
the route:
Route::get('/job/show/{id}/{name}', ['as'=>'website.job.show','uses'=>'HomeController#show']);
in the controller, update the check if the name is equal to the job name (in case it was changed) to prevent duplicate pages url's
public function show($id, $name){
$job = Job::findOrFail($id);
// check here if( $job->name != $name ) {
// redirect 301 to url with the new name
// }
return view('website.job')->with(compact('job'));
}
in the blade.php :
{{$job->name}}
in my controller in my show function in laravel i want the get the id that shows in browser show when i browse it it shows like this
http://localhost:8000/admin/invoices/1
i want to get that "1" and use it in show controller like below
public function show(Invoice $invoice)
{
$clients = Invoice::with('user','products')->get();
$invoice_id = 1;
$invoices = Invoice::with('products')->where('id', '=', $invoice_id)->firstOrFail();
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
}
and put it instead of $invoice_id so when every my client visit this page only sees the related invoice products . thanks you for help
If you're actually getting an instance of Invoice passed to your show method then it likely means you have Route-Model Binding set up for your project. Laravel is looking at the defined route and working out that the ID part (1) should map to an instance of Invoice and is doing the work to grab the record from the database for you.
The Invoice object passed through should refer to an item in your database with the ID of 1, so to get the ID that was mapped in the route you can simply just do:
public function show(Invoice $invoice)
{
echo $invoice->id; // This should be 1
Laravel supports route model binding out of the box these days, but in earlier versions you had to set it up in app/Providers/RouteServiceProvider.php. If you don't want it, try replacing your show method signature with this:
public function show($id)
{
echo $id; // Should be 1
By removing the type-hint you're simply expecting the value that was given in the route parameter and Laravel won't try to resolve it out of the database for you.
Simple way you may try this.
//Define query string in route
Route::get('admin/invoice/{id}','ControllerName#show')
//Get `id` in show function
public function show(Invoice $invoice,$id)
{
$invoice_id = $id;
}
Try using $invoiceId
public function show(Invoice $invoice, $invoiceId)
{
$clients = Invoice::with('user','products')->get();
$invoices = Invoice::with('products')->findOrFail($invoiceId);
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
}
do this if you want to get the url segment in controller.
$invoice_id = request()->segment(3);
if you want this in view
{{ Request::segment(3) }}
Goodluck!
Usually happens when giving a route name different from the controller name
Example:
Route::resource('xyzs', 'AbcController');
Expected:
Route::resource('abcs', 'AbcController');
I want to pass multiple ID's to my controller.First for Categories and second for Food items as show in function show
My Controller is:
public function index()
{
$Foods=Food::all();
$Category=Categories::all();
return view('index.welcome', compact('Foods','Category'));
}
public function show($Food_id,$Category_id)
{
$food = Food::with('restaurant','categories')->findOrFail($Food_id);
$category = Categories::with('food')->findOrFail($Category_id);
return view('index.show', compact('food','category'));
}
My Routes are:
Route::get('index','DetailsController#index');
Route::get('index/{Food_id?}', 'DetailsController#show');
But it returns me error "Missing argument 2 for App\Http\Controllers\Detailscontroller::show()".Where is the problem in this?
You are calling a method which has 2 arguments, but in the route file you're only specifying one parameter.
Maybe your route should look something like this:
Route::get('index/{Food_id?}/{Category_id}', 'DetailsController#show');
Then you would be passing both variables.
Or modify your method declaration and remove the $Category_id parameter, as you are not passing it to the method, like this:
public function show($Food_id)