laravel get Data from table using model in laravel 5.2 - laravel

i want to get vendor_name and user who assign order to vendor in view. But every time i got this error
ErrorException in b6bb559eccdc8a2d45a2d2d6ce89e8e217411386.php line 26:
Trying to get property of non-object (View: E:\xampp\htdocs\ftdindia\resources\views\view\Orders\allorder.blade.php)
in b6bb559eccdc8a2d45a2d2d6ce89e8e217411386.php line 26
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
my codes are given below
Controller for Order
public function allorder(){
$orders = OrderGrid::paginate(100);
return view ('view.Orders.allorder', ['orders' => $orders]);
//dd($orders->all());
}
Here is My Model
class OrderGrid extends model{
protected $table = 'order_grids';
public function vendor() {
return $this->belongsTo(User::class);
}
public function assignedBy() {
return $this->belongsTo(User::class, 'vendor_assigned_by_user');
}
}
Here is my view
#foreach($orders as $order)
<tr>
<td> {{ $i }}</td>
<td>{{ $order->order_id }}</td>
<td>{{ $order->vendor->username }}</td>
<td>{{ $order->assignedBy->username }}</td>
<td>{{ $order->delivery_city }}</td>
<td>{{ $order->delivery_date }}</td>
<td>{{ $order->delivery_time }}</td>
<td>{{ $order->order_statuss }}</td>
<td>{{ $order->order_status }}</td>
<td>
#if(!$order->vendor_id)
Unassigned
#else
Assigned
#endif
</td>
<td></td>
</tr>
<?php $i +=1; ?>
#endforeach

#foreach($orders as $order)
<tr>
<td> {{ $i }}</td>
<td>{{ $order->order_id }}</td>
<td>{{ $order->vendor->username }}</td> inspite of this do <td>{{ !is_null($order->vendorName) ? $order->vendorName->username : null }}</td>
<td>{{ $order->assignedBy->username }}</td> and same here also <td>{{ !is_null($order->assignedBy) ? $order->assignedBy->username : null }}</td>
<td>{{ $order->delivery_city }}</td>
<td>{{ $order->delivery_date }}</td>
<td>{{ $order->delivery_time }}</td>
<td>{{ $order->order_statuss }}</td>
<td>{{ $order->order_status }}</td>
<td>
#if(!$order->vendor_id)
Unassigned
#else
Assigned
#endif
</td>
<td></td>
</tr>
<?php $i +=1; ?>
#endforeach
and in Model
public function vendorName() {
return $this->belongsTo('App\Model\User', 'vendor_id', 'id');
}
public function assignedBy() {
return $this->belongsTo('App\Model\User', 'assigned_by', 'id');
}

The error you are getting relates to how you are calling the vendor() function in the $order collection.
The docs are a good place for a solid understanding on this, but essentially all Eloquent relationships are defined via functions and so you may call those functions to obtain an instance of the relationship without actually executing the relationship queries.
As such it needs to be:
<td>{{ $order->vendor()->username }}</td>
Nothing that we are now chaining vendor() rather than vendor.

Related

How to clear undefined variable Laravel 9.48.0

public function student(){
$users=DB::select('select * from details');
return view('welcome',['users'->$users]);
}
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email}}</td>
<td>{{ $user->status}}</td>
<td>{{ $user->subject}}</td>
</tr>
#endforeach
don't use raw sql query instead you can do it like this
public function student()
{
$users=Details::all();
return view('welcome',compact('users'));
}
or like this
public function student()
{
$users = DB::table('details')
->select('*')
->get();
return view('welcome', compact('users'));
}
and then in your blade you could get it like this
#isset($users)
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->status }}</td>
<td>{{ $user->subject }}</td>
</tr>
#endforeach
#endisset

group data in foreach in laravel

I have this table
here's the index function
use App\GrantLoanAmount;
use App\LoanAmount;
use App\User;
use App\Profile;
public function index(Request $request)
{
$defaultAmount = LoanAmount::where('default', 1)->first();
$grantloanamounts = GrantLoanAmount::with('users','amounts')->get();
return view('loan.grant-loan-amounts.index', compact('grantloanamounts'))
->with('defaultAmount',$defaultAmount);
}
here's my index blade table
#foreach($grantloanamounts as $item)
#if($item->amounts[0]->amount > $defaultAmount->amount)
<tr>
<td>{{ $item->users[0]->email }}</td>
<td>{{ $item->amounts[0]->amount }}</td>
<td>{{ $item->users[0]->name }}</td>
<td class="text-right">action buttons</td>
</tr>
#endif
#endforeach
How can I group the user name to become look like this?
username
amount
amount
amount

Laravel Foreach Looping

I need to display the previous value of the iterated array, here is code
<tr>
<th></th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Delivery Address</th>
<th>Contact No.</th>
<th>Zip Code</th>
<th>Payment Terms</th>
<th>Credit Limit</th>
</tr>
<?php $previousValue = null; ?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue=$value->customer_name }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
#endforeach
I need to display the previous name of Customer in the iteration?
Any idea on how to do this?
Set value at the end , I set blank for first
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue or ""}}</td>
.....
.....
</tr>
<?php $previousValue = $value->customer_name ?>
#endforeach
$customer_count[$key - 1]
Of course you'll need to check if that exists or use the null-coalesce operator or something, so:
#php
$previous = $customer_count[$key - 1] ?? false;
#endphp
Then use:
#if( $previous )
... some stuff ...
#endif
wherever you need to inject.
please try this
<td>{{ (!$loop->first) ? $customer_count[$key - 1]->customer_name : '' }}</td>
You can read more about foreach loop here.
At very first set the previous value as blank and then at the bottom set value in $previousValue
<?php $previousValue = '';?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{ $previousValue }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
<?php $previousValue = $value->customer_name; ?>
#endforeach

Laravel Resource controller not getting delete route on amchor click

In laravel, I have resource controller :
Route::resource('/student','StudentController');
here is my view listing code :
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
Link
</td>
</tr>
conytroller function i have :
public function destroy($id)
{
echo "ok";
}
My problem is that when I click on anchor button(delete ) it not
found the resource route for the delete what I am doing wrong here can
anyone please help me elated this.
when I click on the delete anchor it calls the show function of resource but I want the destroy function to call on click.
destroy function use DELETE method
in your blade view
#foreach($students as $model)
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
DELETE
<form id="delete_form" style="display:none" action="{{ route('student.destroy',$model->id)}}" method="POST" >
{{ csrf_field() }}
<input type="hidden" name="_method" value="delete" />
</form>
</td>
</tr>
#endforeach

Laravel: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

I want to save my results in the database but im getting an error exception.
In my view I have a radio button(array) that gets the result of each student which is present,late,absent,others
Here's my view
<td>{{ $users->student_id }} </td>
<td>{{ $users->student_firstname }} {{ $users->student_lastname }}</td>
<td>{{ Form::radio('result['.$users->student_id.']', 'present' , true) }}</td>
<td>{{ Form::radio('result['.$users->student_id.']', 'late' ) }}</td>
<td>{{ Form::radio('result['.$users->student_id.']', 'absent') }}</td>
<td>{{ Form::radio('result['.$users->student_id.']', 'others') }}</td>
Here's my controller
$attendance = new Attendances();
$attendance->status = Input::get('result');
$attendance->comment = Input::get('comment');
$attendance->save();
Your radio input result will return an array, due to the naming convention you have chosen.
If you wish to save the singular value of the input result, use the following format.
View code:
<td>{{ Form::radio('result', 'present' , true) }}</td>
<td>{{ Form::radio('result', 'late') }}</td>
<td>{{ Form::radio('result', 'absent') }}</td>
<td>{{ Form::radio('result', 'others') }}</td>
If you are expecting multiple values in an array, then you should be looping through the code, and saving each individually:
Controller Code:
foreach (Input::get('result') as $studentId=>$value)
{
$attendance = new Attendances();
$attendance->status = $value;
$attendance->comment = Input::get('comment');
//We should save the student id somewhere.
$attendance->student_id = $studentId;
$attendance->save();
}
Suggestion:
If you wish to save several student's information on the same form, the suggestion below will work great.
View Code:
<td>{{ $users->student_id }} </td>
<td>{{ $users->student_firstname }} {{ $users->student_lastname }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'present' , true) }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'late' ) }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'absent') }}</td>
<td>{{ Form::radio('student['.$users->student_id.'][status]', 'others') }}</td>
<td>{{ Form::text('student['.$users->student_id.'][comment]'}}</td>
Controller Code for saving
//We loop through each student and save their attendance.
foreach (Input::get('student') as $studentId=>$data)
{
$attendance = new Attendances();
$attendance->status = $data['status'];
$attendance->comment = $data['comment'];
//We should save the student id somewhere.
$attendance->student_id = $studentId;
$attendance->save();
}
I had a similar error, where using
$attendance->fill(array with non-existing columns);
sets a non-existing column value, so on calling
$attendance->save();
afterwards, it will throw that error

Resources