I can't get the name property to display in my index always showing error
"Function name must be a string"
Controller
public function index()
{
$accounts = Account::with('student')->get();
return $accounts('Student_id');
$student = Student::find($id, ['id', 'studentname']);
return view('accounts.index',compact('accounts'));
}
Model
protected $fillable = [
'accountname',
'Student_id',
];
public function student() {
return $this->belongsTo(Student::class);
}
View Code:
<tbody>
#foreach ($accounts as $account)
<tr>
<td>{{ $account->accountname }}</td>
<td> {{$account->student->studentname}}</td>
</tr>
#endforeach
</tbody>
I'm trying to display the studentname instead of Student_id using one to many relationship.
this is the Error
Note: but if i changed this {{$account->student->studentname}} to this {{$account->Student_id}} it work but only showing the id not the name.
Remove these lines:
return $accounts('Student_id');
$student = Student::find($id, ['id', 'studentname']);
Also, since you're not using a standard foreign key name, you need to add it to the definition:
public function student()
{
return $this->belongsTo(Student::class, 'Student_id');
}
Also, it's a good idea to follow Laravel naming conventions which you will find in my repo and rename Student_id to student_id. In this case, your relationship will work without defining a foreign key.
Update
The solution for the third issue you have is:
<td> {{ optional($account->student)->studentname}}</td>
Related
I have two table SALARIES and POINTAGES And between them a relationship hasMany belongsTo, I want to display for each POINTAGE a SALARIES corresponds but it gives me empty datatable.
consulter.blade.php
#foreach($pointages as $pointage)
<tr>
<td>{{ $pointage->datep }}</td>
<td>{{ $pointage->chantier }}</td>
<td>{{ $pointage->ouvrage }}</td>
<td>{{ $pointage->nbrj }}</td>
<td>{{ $pointage->solde }}</td>
<td>{{ $pointage->salarie->nom }}</td>
</tr>
#endforeach
Pointage.php
protected $fillable = [
'salarie_id', 'datep', 'solde', 'nbrj' , 'ouvrage' , 'chantier' , 'prime' ,
];
public function salarie(){
return $this->belongsTo('App\Salarie');
}
Salarie.php
public function pointages(){
return $this->hasMany('App\Pointage');
}
pointages migration:
public function up(){
Schema::table('pointages', function (Blueprint $table) {
$table->integer('salarie_id')->unsigned()->after('id');
$table->foreign('salarie_id')->references('id')->on('salaries');
});
}
SalarieController.php
public function consulter()
{
$salaries = Salarie::with('pointages')->get();
$pointages = Pointage::with(["salaries"])->has("salarie")->get();
return view('salarie.consulter', compact('salaries','pointages'));
}
You need to define explicit the relationship functions:
// app\Salarie.php
class Salarie extends Model
{
protected $fillable = ['nome'];
public function pointages(){
return $this->hasMany('App\Pointage','salarie_id','id');
}
}
// app\Pointage.php
class Pointage extends Model
{
protected $fillable = [
'salarie_id', 'datep', 'solde', 'nbrj' , 'ouvrage' , 'chantier' , 'prime' ,
];
public function salarie(){
return $this->belongsTo('App\Salarie');
}
}
And use like below to consulter all of pointages relate with salaries table:
// app\Http\Controllers\SalarieController.php
class SalarieController extends Controller
{
public function consulter()
{
// test your model with this simple query
// $salaries = Salarie::find(1);
// $pointages = $salaries->pointages()->get();
// return view('salarie.consulter', compact('pointages'));
// if the upon test runs well, the follow codes will work
$salaries_ids = Salarie::with('pointages')->pluck('id');
$pointages = Pointage::whereHas('salarie', function($query) use ($salaries_ids) {
$query->whereIn('salarie_id', $salaries_ids);
})->get();
return view('salarie.consulter', compact('pointages'));
}
}
Hope this helps, ask me if you need!
Some things you could try:
I'm not sure if it matters but you could try removing the brackets in de with() like so Pointage::with("salaries")->has("salarie")->get();
Another thing you should check if the primary and foreign keys of Salarie and Pointage are correct. Laravel documentation states the following:
In the example above, Eloquent will try to match the user_id from the Phone model to an id on the User model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id. However, if the foreign key on the Phone model is not user_id, you may pass a custom key name as the second argument to the belongsTo method.
this is may route code
Route::get('/dashboard',['middleware' => 'auth', function () {
$links = App\Website_links::where('website_id',1)->get();
return view('user.dashboard',['links_data'=>$links]);
}]);
and website_link model code is
class Website_links extends Model
{
public $timestamps = false;
protected $table = 'website_links';
public function project()
{
return $this->belongsTo('App\Website','website_id')
->select('project_name','project_status');
}
}
and view code
#foreach($links_data as $links)
<tr>
<td>{{$links['page_url']}}</td>
<td>{{$links['project']}}</td>// here i want to display project_name from website table
</tr>
#endforeach
databse schema:
website : id,project_name,project_status
website_links: id, page_url, website_id //here website_id is forign key.
now its shows page_url correctly but
{{$links['project']}} display {"project_name":"Project1","id":45}
and i want to show project_name value like project1 instead of whole array
{"project_name":"Project1","id":45}
Change
$links['project'] to $link->project->project_name
#foreach($links_data as $links)
<tr>
<td>
{{$links['page_url']}}
</td>
<td>
{{$links->project->project_name}}
</td>// here i want to display project_name from website table
</tr>
#endforeach
And change your query to the following to prevent the N+1 query problem
$links = App\Website_links::with('project')->where('website_id',1)->get();
I'm trying to echo out the name of the user in my article and I'm getting the
ErrorException: Trying to get property of non-object
My code:
Models
1. News
class News extends Model
{
public function postedBy()
{
return $this->belongsTo('App\User');
}
protected $table = 'news';
protected $fillable = ['newsContent', 'newsTitle', 'postedBy'];
}
2. User
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
}
Schema
table users
table news
Controller
public function showArticle($slug)
{
$article = News::where('slug', $slug)->firstOrFail();
return view('article', compact('article'));
}
Blade
{{ $article->postedBy->name }}
When I try to remove the name in the blade {{ $article->postedBy }} it outputs the id, but when I try to add the ->name there it says Trying to get property of non-object but I have a field namein my table and aUser` model. Am I missing something?
Is your query returning array or object? If you dump it out, you might find that it's an array and all you need is an array access ([]) instead of an object access (->).
I got it working by using Jimmy Zoto's answer and adding a second parameter to my belongsTo. Here it is:
First, as suggested by Jimmy Zoto, my code in blade
from
$article->poster->name
to
$article->poster['name']
Next is to add a second parameter in my belongsTo,
from
return $this->belongsTo('App\User');
to
return $this->belongsTo('App\User', 'user_id');
in which user_id is my foreign key in the news table.
If you working with or loops (for, foreach, etc.) or relationships (one to many, many to many, etc.), this may mean that one of the queries is returning a null variable or a null relationship member.
For example: In a table, you may want to list users with their roles.
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->role->name }}</td>
</tr>
#endforeach
</table>
In the above case, you may receive this error if there is even one User who does not have a Role. You should replace {{ $user->role->name }} with {{ !empty($user->role) ? $user->role->name:'' }}, like this:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ !empty($user->role) ? $user->role->name:'' }}</td>
</tr>
#endforeach
</table>
Edit:
You can use Laravel's the optional method to avoid errors (more information). For example:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ optional($user->role)->name }}</td>
</tr>
#endforeach
</table>
If you are using PHP 8, you can use the null safe operator:
<table>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
#foreach ($users as $user)
<tr>
<td>{{ $user?->name }}</td>
<td>{{ $user?->role?->name }}</td>
</tr>
#endforeach
</table>
I implemented a hasOne relation in my parent class, defined both the foreign and local key, it returned an object but the columns of the child must be accessed as an array.
i.e. $parent->child['column']
Kind of confusing.
REASON WHY THIS HAPPENS (EXPLANATION)
suppose we have 2 tables users and subscription.
1 user has 1 subscription
IN USER MODEL, we have
public function subscription()
{
return $this->hasOne('App\Subscription','user_id');
}
we can access subscription details as follows
$users = User:all();
foreach($users as $user){
echo $user->subscription;
}
if any of the user does not have a subscription, which can be a case.
we cannot use arrow function further after subscription like below
$user->subscription->abc [this will not work]
$user->subscription['abc'] [this will work]
but if the user has a subscription
$user->subscription->abc [this will work]
NOTE: try putting a if condition like this
if($user->subscription){
return $user->subscription->abc;
}
It happen that after some time we need to run
'php artisan passport:install --force
again to generate a key this solved my problem ,
I had also this problem. Add code like below in the related controller (e.g. UserController)
$users = User::all();
return view('mytemplate.home.homeContent')->with('users',$users);
Laravel optional() Helper is comes to solve this problem.
Try this helper so that if any key have not value then it not return error
foreach ($sample_arr as $key => $value) {
$sample_data[] = array(
'client_phone' =>optional($users)->phone
);
}
print_r($sample_data);
Worked for me:
{{ !empty($user->role) ? $user->role->name:'' }}
In my case the problem was in wrong column's naming:
In model Product I've tried to access category relationship instance to get it's name, but both column name and relationship had the same name:
category
instead of:
category_id - for column name
category - for relationship
Setting up key name in relationship definition like
public function category():hasOne
{
return $this->hasOne(Category::class,'category');
}
didn't help because as soon as Laravel found property named category gave up on looking for relationship etc.
Solution was to either:
change property name (in model and database) or
change relationship name (Eg. productCategory )
It wasn't an error in my case. However, this happened to me when I was trying to open users.index, because while testing I've deleted some data from the 'STUDENTS' table and in the 'USERS' table, a foreign key ('student_id') represents the 'STUDENTS' table. So, now when the system tries to access the 'USERS' table in which foreign key ('student_id') is null since the value got deleted from the 'STUDENTS' table.
After checking for hours when I realise this, I insert the same data again in the 'STUDENTS' table and this resolved the issue.
I have 3 tables : projects - users - project_user.
This is a ManyOnMany relation so that's why I have a pivot table.
I got them linked via my model using belongsToMany.
Now comes my problem.
I have a table like this:
So I have a project selected, and a table with my users with active/contribute checkboxes.
When the active/contribute checkbox is checked the user will be linked to the project.
So in my head I have something like this:
When the checkbox is checked, it needs to send the user_id including the $this->project_idto the pivot table project_user so it will link the project <->user.
But now the problem is that I have no clue how I'm going to get this into working code.
HTML/Blade:
#foreach($users as $user)
<tr>
<td>
{{$user->firstname}} {{$user->middlename}} {{$user->lastname}}
</td>
<td>
<input name='contribute' type='hidden' value='0'>
{!! Form::checkbox('', '1', false) !!}
</td>
</tr>
#endforeach
Model:
User
public function projects()
{
return $this->belongsToMany('App\Project', 'project_user', 'user_id', 'project_id');
}
Project
public function users()
{
return $this->belongsToMany('App\User', 'project_user', 'project_id', 'user_id');
}
Controller:
public function edit($id, Project $project)
{
$users = User::all();
$project = $this->project->find($id);
return view('project.edit', ['project' => $project, 'id' => 'edit'], compact('users'));
}
public function update(CreateProjectRequest $request, $project)
{
$project = $this->project->find($project);
$project->fill($request->input())->save();
return redirect('project');
}
Tell me if any other code is needed to be provided
You can create Input arrays like below, but you have to check if this user is already associated with this project.
{!! Form::checkbox('contribute['.$user->id.']', '1', $checkifinproject) !!}
Then use sync when you are updating in your Controller.
But before that, because you want only the ids with value 1 from the checkboxes, you have to filter out the zero values, then keep only the keys of the array $request->input('users').
$project = $this->project->find($project);
$project->sync($prepared_ids);
Sync will then delete all the previous user_ids, associated with this project, from the table and save the new ones.
I have a typical model relation. I have the model QR, which hasMany Rating, and a Model Rating, which belongsTo Qr.
Now I want to output the Ratings, which belong to a single qr model, through a foreach loop like this:
<table>
<tr>
<th>ID</th>
<th>UnitID</th>
<th># of Ratings</th>
</tr>
#foreach($qrs as $qr->ratings)
<tr>
<td>{{$qr->id}}</td>
<td>{{$qr->unit_id}}</td>
<td>{{$qr->ratings->count()}}</td>
</tr>
#endforeach
</table>
This is my Controller:
public function index()
{
//
$unit = Unit::all()->first();
$qrs = Qr::all()->first();
return View::make('index')
->with('unit', $unit)
->with('qrs', $qrs);
}
Here are my two Models
Rating.php:
class Rating extends \Eloquent {
protected $guarded = [];
public function qr(){
return $this->belongsTo('Qr');
}
}
Qr.php:
class Qr extends \Eloquent {
protected $guarded = [];
public function unit(){
return $this->belongsTo('Unit');
}
public function ratings(){
return $this->hasMany('Rating');
}
}
I actually want to output the count of ratings, a Qr-Code has. I know it is possible to do it somehow like this:
{{Rating::where('qr_id', $qr->id)->count()}}
But I want to do it somehow like this in the foreach loop
{{ $Qr->rating->count() }}
If this is somehow possible.
I get the relation, if I just output the first() of Qr and then
var_dump($qrs->ratings->toArray())
But I don't know how to get the count Number of ratings in combination with the foreach loop. Any help would be dearly appreciated.
Couple of things wrong here:
// view:
#foreach($qrs as $qr->rating)
// should be:
#foreach($qrs as $qr)
// controller:
$unit = Unit::all()->first();
$qrs = Qr::all()->first();
// this way you get all Units then fetch first Unit from the collection,
// the same with Qrs, so change it to:
$unit = Unit::all(); // do you need it at all?
$qrs = Qr::with('ratings')->get();
This will solve the problem and in the foreach loop you will be able to access $qr->ratings->count() which will be Collection method.
At first you have used this:
#foreach($qrs as $qr->ratings)
You need to change it to this (as already stated in an answer):
#foreach($qrs as $qr)
Then in your index method you have used this:
public function index()
{
$unit = Unit::all()->first();
$qrs = Qr::all()->first();
return View::make('index')->with('unit', $unit)->with('qrs', $qrs);
}
In this case you need to get a collection of QR models and since Unit and Rating are related to Qr then you may use with and get() to get a collection of QR models like this:
public function index()
{
$qrs = Qr::with(array('unit', 'ratings'))->get();
return View::make('index')->with('qrs', $qrs);
}
Then you'll be able to loop the Qr models in your view like this:
#foreach($qrs as $qr)
<tr>
<td>{{ $qr->id }}</td>
<td>{{ $qr->unit_id }}</td>
<td>{{ $qr->ratings->count() }}</td>
</tr>
#endforeach