Pagination in laravel 5 with realtion on where condition - laravel

Model smsHeader
protected $fillable = [
'type',
'imei',
'login',
'ver'
];
public function detail()
{
return $this->hasMany('App\Model\smsDetail', 'id_header');
}
Controller
$smsheader = smsHeader::orderBy('id', 'desc')->where('login', $user->email)->paginate(20);
return view('app.inbox')->with('smsheader', $smsheader);
Views
#foreach($smsheader as $header)
#foreach($header->detail->where('type', '1') as $detail)
<tr>
<td>{{$detail->number}}</td>
<td>{{$detail->name}}</td>
<td>{{$detail->text}}</td>
<td>{{$detail->date}}</td>
</tr>
#endforeach
#endforeach
but the result of pagination it was error, how i can set paginition in where smsdetail condition....??
sorry for my bad english

The error is showing for this line $header->detail->where('type', '1')
you can access $header->detail within foreach if you want to use where condition then you need to use get() at the end,like below
#foreach($smsheader as $header)
#foreach($header->detail->where('type', '1')->get() as $detail)
<tr>
<td>{{$detail->number}}</td>
<td>{{$detail->name}}</td>
<td>{{$detail->text}}</td>
<td>{{$detail->date}}</td>
</tr>
#endforeach
#endforeach

Laravel Queries within Blade views do not automatically execute. They simply prepare another QueryBuilder instance. You still need to run one of the "execute" methods (that will run PDOStatement::execute() underneath). These methods are:
first() (returns first result from result set as single object)
get() (which returns a Collection (ArrayObject with some added extra functionality))
paginate($numberPerPage) (which returns a Collection with extra meta information to pass to a $links property accessible via $collection->links())
In your example:
#foreach($smsheader as $header)
#foreach($header->detail->where('type', '1')->get() as $detail)
<tr>
<td>{{$detail->number}}</td>
<td>{{$detail->name}}</td>
<td>{{$detail->text}}</td>
<td>{{$detail->date}}</td>
</tr>
#endforeach
#endforeach

Related

get access to collection laravel

I am a new learner of the laravel framework working on 3 models
customer,
accidents,
License
where the relations are as follows:
public function License()
{
return $this->hasOne(license::class,'driver_id');
}
public function cars()
{
return $this->hasMany(car::class);
}
In the controller I do like this:
public function AdminCustomers()
{
$customers = Customer::with('cars')->with('License')->get();
return view('admin.AdminCustomers',compact('customers'));
}
now I want to display all 3 models' data on view.blade page
<tbody>
#foreach($customers as $customer)
<tr>
<td>{{$customer->id}}</td>
<td>{{$customer->name}}</td>
<td>{{$customer->adderss}}</td>
<td>{{$customer->mobileNo}}</td>
<th>{{$customer->License->id}}</th>
<th>{{$customer->License->Exp}}</th>
<td>{{$customer->cars->id}}</td>
<td>{{$customer->cars->color}}</td>
<td>{{$customer->cars->model_no}}</td>
<td>{{$customer->cars->company}}</td>
</tr>
#endforeach
</tbody>
But it doesn't work and I didn't know where is the problem
the error Exception
Property [id] does not exist on this collection instance.
$customer->cars will return a collection as the relationship is hasMany so you need to loop over the collection in the view
#foreach($customers as $customer)
<tr>
<td>{{$customer->id}}</td>
<td>{{$customer->name}}</td>
<td>{{$customer->adderss}}</td>
<td>{{$customer->mobileNo}}</td>
<th>{{$customer->License->id}}</th>
<th>{{$customer->License->Exp}}</th>
#foreach($customer->cars as $car)
<td>{{$car->id}}</td>
<td>{{$car->color}}</td>
<td>{{$car->model_no}}</td>
<td>{{$car->company}}</td>
#endforeach
</tr>
#endforeach
check the name in the corrisponding table on db, and if its different from "id" specify it into the model, as
protected $primaryKey = 'xxx';

Property [name] does not exist on this collection instance. Eloquent Laravel

I have a potentially basic problem for Eloquent. I want to show only the name corresponding with Role of that user. I used the Eloquent Relationship ManytoMany. But I can't get that to work.
I tried point to the name using the $users parameter but this didn't work.
My project has three files such as Role model, StudentController, find_username view.
Thank you.
Role Model:
class Role extends Model
{
protected $fillable = ['id','role_name','role_level','role_note'];
public function users()
{
return $this->belongsToMany('App\User', 'role_users', 'user_id', 'role_id');
}
}
My StudentController:
class StudentController extends Controller
{
public function find_username()
{
$roles = Role::all();
foreach($roles as $role)
{
echo $role->users . '<br>';
}
return view('find_username',['roles'=> $roles]);
}
}
My find_username view:
#extends('master')
#section('content')
<div class="row">
<table class="table">
<thead>
<tr>
<th>ID Role</th>
<th>Role name</th>
<th>Role level</th>
<th>Role note</th>
<th>User name</th>
</tr>
</thead>
<tbody>
#foreach ($roles as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['role_name']}}</td>
<td>{{$item['role_level']}}</td>
<td>{{$item['role_note']}}</td>
<td>{{$item->users}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
You have many roles. Each of these roles have many users. Your table will be fine as it is to show the name of the role, roll level, and role note, because these are on the looped $item variable individually.
The problem is that while the $item holds those singular fields, the $item->users is another collection of user objects. Which means you have added another dimension onto your table. You might have 1 or 100 users to fit inside that <tr>.
To demonstrate, try this code, specifically adding in all the users names (adding the parameter to each user object in a new loop):
#foreach ($roles as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['role_name']}}</td>
<td>{{$item['role_level']}}</td>
<td>{{$item['role_note']}}</td>
#foreach($item->users as $user)
<td>
{{$user->name}}
{{$loop->last?"":" | "}} //<-- just to separate names for this test
</td>
#endforeach
</tr>
#endforeach
If you have your models and relations set up correctly, this will show you your users for each role. However, as you can see, this may not be the best way to display this data. You might have dozens of users per role. You may wish to consider making a totally separate table for each role's users. Maybe click on something for the role on this table and bring up a new list or table of those users who have the role.
HTH

Select query with relationship laravel one to many

I have a relational mode like this:
Model Siswa
public function kelengkapan()
{
return $this->belongsTo('Modules\PesertaDidik\Entities\SiswaKelengkapan');
}
Model SiswaKelengkapan
public function siswas()
{
return $this->hasMany('Modules\PesertaDidik\Entities\Siswa');
}
I want to use select query with relational
in this controller:
$siswa = Siswa::select('nama', 'nisn', 'tempat_lahir', 'tanggal_lahir', 'jk')->with('kelengkapan')->get();
return view('pesertadidik::crud.index', compact('siswa'));
this is my blade...
<td>{{$data->kelengkapan->kelas_masuk}}</td>
but an error in the blade like this
Trying to get property 'kelas_masuk' of non-object
I want to show kelas_masuk in the table,
How to use the select query with relation eloquent?
There is no issue with your select query
You are trying to retrieve the data on null that's why you get Trying to get property 'kelas_masuk' of non-object
Controller
$siswa = Siswa::select('nama', 'nisn', 'tempat_lahir', 'tanggal_lahir', 'jk')->with('kelengkapan')->get();
return view('pesertadidik::crud.index', compact('siswa'));
Blade file
Assume you are using foreach or forloop
<td>{{ $data->kelengkapan->kelas_masuk ?? '--' }}</td> //coalescing operator
Or
<td>
#if(!is_null($data->kelengkapan))
{{ $data->kelengkapan->kelas_masuk }}
#endif
</td>
Here you are used get() method so it's return all the rows
$siswa = Siswa::select('nama', 'nisn', 'tempat_lahir', 'tanggal_lahir', 'jk')->with('kelengkapan')->get();
return view('pesertadidik::crud.index', compact('siswa'));
in your blade
<td>{{$siswa[0]->kelengkapan->kelas_masuk}}</td>
You can also use foreach loop.
#foreach($siswa as $data)
<tr>
<td>{{$data->kelengkapan->kelas_masuk}}</td>
</tr>
#endforeach

Laravel — Loop all data from multiple tables

Good day. I have researched about it, but nothings matched with my problem. Here's the scenario. I have courses table, and 3 more tables, assignments, quizzes, and reports. Now, what I want is, to get all of the records from the table assignments, quizzes, and reports and loop through all the records below a specific course.
Example Output: The order should be according to the first created item.
Course1
Assignment1
Quiz1
Quiz2
Assignment2
Report1
How can I do that with three different tables? I know how to use the basic many to many relationship, but for this. I really got stacked. Need help guys.
Note: I'm using Laravel5.1
Update1 — Course.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $fillable = [];
public function assignments(){
return $this->belongsToMany('Assignment');
}
public function quizzes(){
return $this->belongsToMany('Quiz');
}
public function reports(){
return $this->belongsToMany('Report');
}
}
UPDATE2 as per request
<div class="table">
<table class="tabled">
<thead>
<tr>
<th>x</th>
</tr>
</thead>
<tbody>
#foreach($course_items as $course_item)
<tr>
<td>
{{ $table_name }} {{ $course_item->name }}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Based on the limited information available from your question, you will need to merge your relations into a single collection.
Using unions would be a better solution, if feasible, to improve efficiency, but hey-ho.
// Prepare not deleted scope
// I would suggest using Laravel's SoftDeletes, as it'll be better than writing your own implementation. But if not, you would be better to create this as a reusable scope on your model
$notDeletedScope = function ($query) {
return $query->where('deleted', 0);
};
// Find course with non-deleted relations
$course = Course::with([
'assignments' => $notDeletedScope,
'reports' => $notDeletedScope,
'quizzes' => $notDeletedScope,
])
->findOrFail($id);
// Combine relations into single collection
$merged = collect($course->assignments);
$merged = $merged->merge(collect($course->quizzes));
$merged = $merged->merge(collect($course->reports));
// Sort merged relations by created_at
$merged = $merged->sortBy('created_at');
#foreach ($merged as $relation)
#if ($relation->type === 'assignment')
// ...
#elseif ($relation->type === 'quiz')
// ...
#elseif ($relation->type === 'report')
// ...
#endif
#endforeach
Add to each model:
public function getTypeAttribute()
{
return snake_case(substr(strrchr(get_class($this), '\\'), 1));
}

I am trying to delete a particular record from the table in Laravel 5.3

My view page code is
#foreach($clients as $client)
<tr>
<td>{{ $client->client_id }}</td>
<td>{{ $client->ip_address }}</td>
<td>{{ $client->netmask }}</td>
<td>
<a href= "del/{{$client->id}}"><button type = "button" class = "btn btn-danger ">
Delete
</button>
</td>
</tr>
#endforeach
My controller code for destroy method is :
public function destroy($id)
{
$clients = Subnet_behind_client::findOrFail( $id );
$clients->delete();
return view('view2',compact('clients'));
}
My route file is:
Route::get('del/{id}', 'Subnet_Behind_ClientController#destroy');
I am able to view the records in a table in my view page but I am unable to delete a record from that table.
Your code looks good to me.
By the way, if you are deleting a record using its primary key then you can use destroy()
https://laravel.com/docs/5.3/eloquent#deleting-models
Subnet_behind_client::destroy( $id );
This way you don't need to fetch the record you want to delete. It will optimize the performance a bit.
You're trying to call delete() method onto a collection of records returned from findOrFail() method.
By doing this you can't access the query builder as now you have an Collection instead of a QueryBuilder.
To make it work, you can do it like this:
$clients = Subnet_behind_client::findOrFail( $id )->delete();
return view('view2',compact('clients'));
Hope this helps!
Can you please use this way:
First of all print the data if exist or check for existing entry.
$clients = Subnet_behind_client::where('id', $id)->get();
// dd($clients);
if(count($clients)>0){
Subnet_behind_client::where('id', $id)->delete();
}
I think this trick will help you.
Make sure also if you forgot to mention define top of the controller:
use App\Subnet_behind_client;
This should work:
Subnet_behind_client::destroy( $id );
And for deleting, I would suggest to use DELETE request instead of GET.

Resources