i am showing data from database to show.blade.php but this problem is showing
following is the code:
BlockController
public function show(Blog $id)
{
//
$blogs = Blog::findOrfail($id);
return view('blog.show', compact('blogs'));
}
show.blade.php
#section('content')
<h3>{{ $blogs->title }}</h3>
#endsection
output of dd($blogs):
image
i tried all the options that i get but still not working..
With that method signature $id is a Model instance. Eloquent Models implement Illuminate\Contracts\Support\Arrayable. If you pass something that is Arrayable or is an array to find, which findOrFail is calling, it will treat this like you are looking for multiple records and will return a Collection.
You have a Collection and not a Model instance.
If your method signature didn't type hint the Model it would make more sense as you would have the 'id'. If you are type hinting Models for those methods usually you would be using Route Model Binding.
Route Model Binding:
public function show(Blog $blog)
{
// $blog is the matching record
}
Without Model Binding and just getting the parameter:
public function show($blog)
{
// $blog is just the 'id' or what ever you decided to pass in the URL
}
Your routes have a parameter named blog because your resource is named 'blog'. For Implicit Route Model Binding to be in place you have to match the parameter name to the parameter name in the signature.
In your function show(Blog $id), $id is of instance Blog::class
What you need to do next $blogs = Blog::findOrfail($id->id);
Related
I am using Laravel to fetch records from the database for which I have created an apiResource controller. I have setup the following code inside routes.
Route::apiResource('/MyController',MyController::class)->middleware('auth:api');
In MyController.php my code to display a specific data is:
/**
* Display the specified resource.
*
* #param \App\Models\ContentType $contentType
* #return \Illuminate\Http\Response
*/
public function show(MyModel $Model)
{
// show content type info
return response([
'data' => new MyControllerResource($Model)
],200);
}
I get the data when I place an api call like this:
http://localhost:8000/api/MyController/1
What I want is a record getting fetched by passing other field value instead of id in the route. For example.
http://localhost:8000/api/MyController/mypost
Any idea how can I achieve this?
The route key name defaults to id for all models. You will want to update this to name or whatever field "mypost" is by adding a getRouteKeyName() method.
<?php
namespace App;
...
class Post extends Model
{
public function getRouteKeyName()
{
return 'name';
}
...
}
You are using route model binding. And in laravel its default behaviour is to find model with id and return collection. It will not search for any other field. Of course you can change this behaviour but it can only search data by one field. To change this behaviour use getRouteKeyName method in model like:
public function getRouteKeyName()
{
return 'another_field_in_my_table';
}
You can also use explicit binding like:
Route::bind('MyController', function($value) {
return \App\MyModel::where('id', $value)->orWhere('another_field_in_my_table', $value)->first();
});
To learn more about explicit binding check docs.
You'll have to define route separately. You can group the routes by controller and middleware though. And once done, then, Inside your route, you need to change to this:
Route::get('/show/{post:columnName}', [ MyController::class, 'show' ])->middleware('auth:api');
Now your data will be fetched on the basis of your column name defined in the route.
I have a route like the following.
Route::get('/articles/{articleSlug}' ,
[App\Http\Controllers\ArticleController::class, 'single']);
And the method of single() at ArticleController class goes here:
public function single($slug)
{
$article = Article::where('slug',$slug)->first();
$article->increment('viewCount');
return view('home.article',compact('article'));
}
Now I wish to use Route Model Binding for finding this data from the articles table based on the column slug. But as I know, Route Model Binding finds data based on the id. So how to change Route Model Binding finding data from id to slug ONLY for ArticleController.php (meaning that the other Controller classes can work with id as route model binding)?
In case you want to use other model field as the biding attribute instead of id you can define a getRouteKeyName which return the name of the field which must be use
class Article extends Model {
// other methods goes here
public function getRouteKeyName() {
return 'slug';
}
}
Or you can pass the field name directly when you define the route like this
Route::get('/articles/{article:slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
With this code inside of your controller you must ensure that the name provide as parameter in the route definition match the name of the controller argument
public function single(Article $article)
{
$article->increment('viewCount');
return view('home.article',compact('article'));
}
Your controller is already set up, all you need to do is change your variable name to $slug in the route, and I believe that should be enough:
Route::get('/articles/{slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
change your route to this:
Route::get('/articles/{article:slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
and then inject the Article model to your controller function and let laravel do the rest for you:
public function single(Article $article)
{
$article->increment('viewCount');
return view('home.article',compact('article'));
}
you can customize route model bindings directly in the route definition:
past given code in app/model/Article.php:
public function getRouteKeyName()
{
return 'slug';
}
2.when you use slug change route to
Route::get('/articles/{article:slug}' , [App\Http\Controllers\ArticleController::class, 'single']);
to use id sample change slug to id
Route::get('/articles/{article:id}' , [App\Http\Controllers\ArticleController::class, 'single']);
you can add bind method to your model boot() like this
public function boot()
{
Route::bind('article', function ($value) {
return Article::where('slug', $value)->firstOrFail();
});
}
to learn more about it read this section in the Laravel docs
https://laravel.com/docs/9.x/routing#customizing-the-resolution-logic
As mentioned in the title I get the error "Property [id] does not exist on this collection instance." Only when I run the code online here are my relevant codes.
1-EmployeeController (browser tells me that the error is here the second line)
public function show(Employee $employee)
{
$employee = Employee::find ($employee);
$edocument = EDocument::where ('employee_id',$employee->id)->first();
return view ('employee.show')->withEmployee($employee)->withEdocument($edocument);
}
2-show.blade.php
<div class="jumbotron">
<h1>{{$employee->name}} ({{$employee->position}})</h1>
#if (isset($edocument))
Go To Employee Database Page
#else
<p class="lead bg-danger">Employee documents are not uploaded</p>
#endif
Create Employee Contract
if anyone can explain to me this error in more details that would be great also. thanks
ps.. this is my first laravel project (;
You use route model binding in your controller method to get the Employee model. But you also run a find, which would fail since you're passing the model instead of the id. Do as one of the codes shown below and don't mix them.
Do this if you want to use route model binding.
public function show(Employee $employee)
{
$edocument = EDocument::where ('employee_id', $employee->id)->first();
return view ('employee.show')->with(compact('employee', 'edocument'));
}
Do this if you want to pass the employee id and fetch the model in controller.
public function show($employee)
{
$employee = Employee::find($employee);
$edocument = EDocument::where ('employee_id', $employee->id)->first();
return view ('employee.show')->with(compact('employee', 'edocument'));
}
Maybe this can help you. Why don't you pass the information in the controller using -
return view('employee.show', ['employee' => $employee, 'edocument'=>$edocument]);
It worked for me. (Do not have to change anything in the show.blade .php)
I have two models: MenuCategory and MenuItem, I want to display MenuItem data on my blade page along with its MenuCategory. I know its possible to do this by adding it to the return data in my controller however I would like to do it leveraging Eloquent instead, however I receive errors.
Here are my codes:
MenuCategory model
public function items()
{
return $this->hasMany('App\MenuItem');
}
MenuItem model
public function category()
{
return $this->belongsTo('App\MenuCategory');
}
Controller
public function show($id)
{
$item = MenuItem::findOrFail($id);
return view('menu.admin.single', compact('item'));
}
Blade Page
{{ $item->category->name }}
UPDATE:
Table menu_item
id
name
menu_category_id
Table menu_category
id
name
When using all the above I get the following error:
Trying to get property of non-object
This error is due to the naming convention of Eloquent.
Provide the optional foreign key variable in your relationship method to make it work, ie.
$this->belongsTo('App\MenuCategory', 'menu_category_id');
Probably every Item doesn't contain a related category but to make sure you may try something like this, it'll try to retrieve the name only if there is a related category is available:
{{ $item->category ? $item->category->name : 'No Name or empty string' }}
Alternatively you may try something like this:
$item = MenuItem::has('category') // check if there is a related category
->with('category') // if yes then load it with that category
->findOrFail($id);
You used a different foreign key than Laravel expect so explicitly mention it like:
public function category()
{
return $this->belongsTo('App\MenuCategory', 'menu_category_id', 'id');
}
I have a dynamic property user in my model:
class Training extends Model
{
...
public function user()
{
return $this->belongsTo('App\User');
}
}
And I can easy get username in controller like this:
Training::find(1)->user->name
But I don't know how to perform the same in view. I tried this:
Controller:
return view('training/single', Training::find(1));
View:
{{ $user->name }};
but without success, I'm getting error Undefined variable: user. So it's look like I can't access dynamic property in view.
Any idea how can I use dynamic property in views?
I fear that's not really possible. There's no way to set the $this context in your view to the model. You could convert the model into an array with toArray() but that would include the related model and you would have to access it with $user['name'].
I personally would just declare the user variable explicitly:
$training = Training::find(1);
return view('training/single', ['training' => $training, 'user' => $training->user]);
Use eager loading
return view('training/single', Training::with('user')->find(1));