cannot use hasOne relationship to place restriction - laravel

i have hasone relationship between models "member" and "loan". i throws "trying to get property of non object error
i have tried on hasMany and it all works but it has to be in hasOne, there is no restriction to stop multiple loan being issued to a member.
Member.php
use Illuminate\Database\Eloquent\Model;
class Member extends Model
{
protected $fillable=['name','address','phone'];
public function loan()
{
return $this->hasOne(Loan::class,'member_id','id');
}
public function savings()
{
return $this->hasMany(Saving::class,'member_id','id');
}
}
Loan.php
use Illuminate\Database\Eloquent\Model;
class Loan extends Model
{
protected $fillable = [
'amount',
'interest',
'status',
'member_id',
'loan_type_id',
'interest_type_id',
'loan_payment_type_id'
];
public function member()
{
return $this->belongsTo(Member::class,'member_id','id');
}
}
member.blade.php
<td>{{$member->name}}</td>
<td>{{$member->address}}</td>
<td>{{$member->phone}}</td>
<td>
Delete
Edit
</td>
<td>
{{$member->loan->amount}}
</td>

Maybe all of your member don't have a loan. that's why error shown, Try to check before echo,
<td>
#if($member->loan) // add this line code
{{ $member->loan->amount }}
#endif // add this line code
</td>

Try
{{$member->loan['amount']}}

Related

Trying to get property 'first_name' of non-object

I am trying to fetch digitizing order related to the user but unfortunately, I am facing an error.
Please see this error: https://flareapp.io/share/VmeWJ47Q
Controller
public function index()
{
$data=
[
'digitizings'=>Digitizing::with('user')->paginate(8)
];
return view('front_end.profile.digitizing.digitizing_view_order',$data);
}
User Model
class User extends Authenticatable
{
use Notifiable;
public function digitizing()
{
return $this->hasMany('App\Digitizing','user_id');
}
}
Digitizing Model
class Digitizing extends Model
{
protected $fillable = ['id','order_name','height','width','urgent','image',
'order_placement','required_format','order_fabric','instruction','user_id'];
protected $table ="digitizing_orders";
public function user()
{
return $this->belongsTo('App\User');
}
}
HTML view
#foreach($digitizings as $key =>$digitizing)
<tr>
<td>DPO# {{$digitizing->id}}</td>
<td>{{$digitizing->created_at}}</td>
<td>{{$digitizing->order_name}}</td>
<td>{{$digitizing->user->first_name}}</td>
<td>{{$digitizing->user->email}}</td>
<td>{{$digitizing->released_date ?? 'processing'}}</td>
<td>View
</td>
</tr>
#endforeach
Does every Digitizing-Entry has set a valid user_id in database? Try checking eager loaded data is set before accessing it.
Try to send the data to the view like this:
public function index()
{
$digitizings = Digitizing::with('user')->paginate(8);
return view('front_end.profile.digitizing.digitizing_view_order',$digitizings);
}

How to display a column of different tables using the pivot table?

I am beginner to Laravel and now I am doing this project but I am stuck in this point for a couple days. Here is what I wish do, I would like to display the day and timeslot labels selected by the tutor!
here is my tables and their relationships
https://imgur.com/WMMwhHK
Day Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Day extends Model
{
protected $guarded = ['id', '_token'];
}
Timeslot Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Timeslot extends Model
{
protected $guarded = ['id', '_token'];
public function Tutor()
{
return $this->hasMany(Tutor::class);
}
}
Tutor_availability Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tutor_availability extends Model
{
protected $fillable = ['tutor_id', 'timeslot_id', 'day_id'];
protected $guarded = ['id', '_token'];
public function tutor()
{
return $this->belongsTo('App\Tutor');
}
public function day()
{
return $this->belongsTo('App\Day');
}
public function timeslot()
{
return $this->belongsTo('App\Timeslot');
}
}
tutor_availabilities Migration
public function up()
{
Schema::create('tutor_availabilities', function (Blueprint $table) {
// $table->bigIncrements('id');
$table->integer('day_id')->unsigned();
$table->foreign('day_id')->references('id')->on('days')->onDelete('cascade');
$table->integer('timeslot_id')->unsigned();
$table->foreign('timeslot_id')->references('id')->on('timeslots')->onDelete('cascade');
$table->integer('tutor_id')->unsigned();
$table->foreign('tutor_id')->references('id')->on('tutors')->onDelete('cascade');
$table->timestamps();
$table->primary(['day_id', 'timeslot_id', 'tutor_id']);
});
}
TutorController
public function index()
{
$tutors = Tutor::latest()->paginate(5);
$tutor_availabilities = Tutor_availability::all();
return view('tutors.index', [
'tutors' => $tutors,
'tutor_availabilities' => $tutor_availabilities
]);
}
View
<tr>
<th>Days</th> <th> Timings</th>
</tr>
<tr>
<td>
{{$tutor_availabilities->day->label}}
</td>
<td>
{{$tutor_availabilities->timeslot->label}}
</td>
</tr>
Put this in your Tutor_availability model
public function day()
{
return $this->belongsTo(Day::class);
}
public function timeslot()
{
return $this->belongsTo(Timeslot:class);
}
Then in controller
public function index()
{
$tutor_availabilities = Tutor_availability::all();
return view('tutors.index',
compact('tutor_availabilities')) ;
}
Then in view
<table>
#foreach ($tutor_availabilities as $tutor_available)
<tr><td>
{{$tutor_available->day->label}}
</td>
<td>
{{$tutor_available->timeslot->label}}
</td></tr>
#endforeach
</table>

laravel 5.7 Relationships belongsto hasmany error

I'm new to laravel, I'm beginning with laravel 5.7. I have two model.
Modelo categoria:
class categoria extends Model
{
public function indicadores()
{
return $this->hasMany('App\Indicador');
}
}
Modelo Indicador:
class Indicador extends Model
{
public function categoria()
{
return $this->belongsTo('App\categoria');
}
}
In the controller, I look for all Indicador
class IndicadorController extends Controller {
public function index() {
$indicadores = DB::table('indicadors');
return view('indicador.index', ['indicadores' => $indicadores]);
}
When I try to show the category to which the indicador belongs,
<tbody>
#foreach ($indicadores as $indicador)
<tr>
<td>{{ $indicador->categoria->nombre }}</td>
</tr>
#endforeach
</tbody>
I get the following error
Undefined property: stdClass::$categoria (View:resources\views\indicador\index.blade.php)
And I don't understand why. Thanks in advance
Using the DB facade returns an instance of Illuminate\Database\Query\Builder
so the relationship will not be accessible.
You instead want an Eloquent model instance which you can use the model directly:
use App\Indicador;
...
$indicadores = Indicador::all();
or as #Chris suggests, eager load:
$indicadores = Indicador::with('categoria')->get();
Use Eloquent instead of the query builder:
class IndicadorController extends Controller {
public function index() {
$indicadores = Indicador::all();
return view('indicador.index', ['indicadores' => $indicadores]);
}
}

LARAVEL 5.6 - error Trying to get property 'nsc' of non-object

I want to echo value of NSC from table Group Class but i get this error Trying to get property 'nsc' of non-object
Table Item Name
Table Group Class
Model Item Name
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ItemName extends Model
{
protected $table = 'tbl_item_name';
protected $fillable = [
'inc',
'item_name',
'short_name',
'definition_eng',
'definition_ind'
];
public function GroupClass()
{
return $this->belongsTo('App\GroupClass', 'nsc', 'inc');
}
}
Model Group Class
namespace App;
use Illuminate\Database\Eloquent\Model;
class GroupClass extends Model
{
protected $table = 'tbl_group_class';
protected $fillable = [
'inc',
'nsc',
'description',
'main_group'
];
public function ItemName()
{
return $this->belongsTo('App\ItemName', 'inc', 'nsc');
}
}
Blade
<td>{{ $ItemName->GroupClass->nsc }}</td>
Please help to solve this problem, thank you so much
You should try this::
Blade
<td> {{ isset($ItemName->GroupClass) ? $ItemName->GroupClass->nsc : '' }} </td>
Model Item Name
public function GroupClass()
{
return $this->belongsTo('App\GroupClass', 'inc', 'nsc');
}
Model Group Class
public function ItemName()
{
return $this->belongsTo('App\ItemName', 'nsc', 'inc');
}

laravel, displaying data from another table

I am trying to solve following problem:
I have 3 tables : passports ,statuses and passport_statuses, (passport_statuses table is the pivot table formed because of many to many relationship between passports and statuses. The model are like below:
passport model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Passport extends Model
{
protected $fillable=[
'Full_Name',
'Date_of_Birth',
'Passport_Number',
'comments',
'Delivered_to_owner'
];
public function status()
{
return $this->belongsToMany('App\Statuses',"passport_statuses","passport_id","statuses_id")->withTimestamps();
}
public function LatestStatus($query)
{
//dd($this->status[0]);
$allStatus=$this->status()->orderBy('updated_at')->first();
dd($allStatus);
//return $allStatus[0]->Status_Name;
//dd($allStatus[0]->attributes["Status_Name"]);
//dd($this->status()->get());
//return $allStatus[0]->attributes["Status_Name"];
//dd($allStatus);
}
}
Statuses model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Statuses extends Model
{
protected $fillable=[
'Status_Name'
];
public function passport()
{
return $this->belongsToMany('App\Passport')->withTimestamps();
}
}
passportstatus model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PassportStatus extends Model
{
protected $fillable=[
'passport_id',
'statuses_id'
];
}
So I was able to save the passport status in the pivot table. Now I a trying to display the the latest status of that passport in the index page.
index page snapshot
saving passport details
my index view
#extends('admin.adminmaster')
#section('content')
<h2>Passport list </h2>
<a class="btn btn-primary" href="passport/create" role="button">+ Add New Passport</a>
<hr>
<table style="width:100%">
<tr>
<th>Name</th>
<th>Passport Number</th>
<th>Status</th>
</tr>
#foreach($passports as $passport)
<tr>
<td><h4> {{$passport->Full_Name}}</h4></td>
<td>{{$passport->Passport_Number}}</td>
{{--<td>{{dd($passport->status())}}</td>--}}
<td><a class="btn btn-warning" href="{{action('PassportController#edit', [$passport->id])}}" role="button">Edit</a>
<td><a class="btn btn-danger" href="{{action('PassportController#destroy',[$passport->id])}}" role="button"><span class="glyphicon glyphicon-trash"></span> Delete</a>
</td>
</tr>
#endforeach
</table>
#endsection
Controller
public function index()
{
$passports=Passport::latest()->get();
return view('admin.passport.index')->with('passports',$passports);
}
public function create()
{
$statuses=Statuses::lists('Status_Name','id');
return view('admin.passport.create')->with('statuses',$statuses);
}
public function store(Request $request)
{
$passport=Passport::create($request->all());
$passport->status()->attach($request->input('Status_Name'));
return redirect('admin/passport');
}
public function show($id)
{
$passports=Passport::findorFail($id);
return view('admin.passport.show')->with('passports',$passports);
}
You can add a function to your model that gets the latest status, and then you can use that function inside your view. This new function is not a new relationship, it is just a standard function.
class Passport extends Model
{
public function status() {
return $this->belongsToMany('App\Statuses',"passport_statuses","passport_id","statuses_id")->withTimestamps();
}
// this is not a relationship. just a standard method on the model.
public function latestStatus() {
return $this->status()->latest()->first();
}
}
In your view:
<td>{{ $passport->latestStatus()->Status_Name }}</td>

Resources