How to view the data from two or more Modal and view the that data just three data with descending to front-end ?
i was try with this code but not success..
class FrontController extends Controller
{
public function index()
{
$lowongans = Lowongan::orderBy('id', 'desc');
$agendas = Agenda::orderBy('id', 'desc')
->take(3)
->get();
return view('index', compact('lowongans','agendas'));
}}
in the index.blade i try to access the data
<div class="post-row1">
#foreach($agendas as $agenda)
{{$agenda->name}}
#endforeach
</div>
<div class="post-row2">
#foreach($lowongans as $lowongan)
{{$lowongan->name}}
#endforeach
</div>
its not success, just Agenda:: can access but not with the Lowongan::
You need to call get().
$lowongans = Lowongan::orderBy('id', 'desc')->get();
Laravel only prepares the query until you actually call methods like get, first, etc.
Related
I have a basic laravel 9 crud app.
There I have two tables in my db. companies and employees.
I'm trying to display single-user details on my employees.show blade
All my data is displayed correctly but when I try to print user's company, I'm kept getting the following error.
Property [company] does not exist on the Eloquent builder instance.
Following is my employee model
protected $fillable = [
'first_name', 'last_name', 'email', 'phone', 'company_id'
];
public function company()
{
return $this->belongsTo(Company::class);
}
and the following is the show function in my controller
public function show(Employee $employee)
{
$data = Employee::with('company')->where('id', '=', $employee->id);
return view('employees.show', compact('employee','data'));
}
and this is my show.blade
<div class="row mb-4">
<label class="col-sm-2 col-label-form"><b>Company</b></label>
<div class="col-sm-10">
{{ $data->company->name }}
</div>
</div>
How can I display the company properly...
I have company_id as a foreign key in my employees' table...
I have following when I dd $data
You're returning a QueryBuilder from the Employee query in your show() function. You want to add ->get() to return a collection or Employee records:
public function show(Employee $employee)
{
$data = Employee::with('company')->where('id', '=', $employee->id)->get();
return view('employees.show', compact('employee','data'));
}
I think the problem in Query for get data. In employees.show you only show one data then and in controller you got collection of data as array. So your single data element can't access because it is array and for execution you need to add foreach loop for that.
Here you need to change query method from get() to first() and it will resolve your issue.
Your show function code looks like this
public function show(Employee $employee)
{
$data = Employee::with('company')->where('id', '=', $employee->id)->first();
return view('employees.show', compact('employee','data'));
}
Your employees.show blade looks like this
<div class="row mb-4">
<label class="col-sm-2 col-label-form"><b>Company</b></label>
<div class="col-sm-10">
{{ $data->company->name }}
</div>
</div>
Loading all the data at once will make the page load slower. Is there any way to load only 10 records and load the others if the user clicks to page 2 or something?
Below is the code I used to load all data. It works just fine but when I import 5,000 records, it crashes the page to error 500 because of the slow loading
public function index()
{
$users = User::all();
$documents = Receiving::where('draft', '=', 0)->get();
return view('documents/index', compact('users', 'documents'));
}
when I use paginate(); it would only show 1 page
You should use paginate with parameter, like this:
$documents = Receiving::where('draft', '=', 0)->paginate(20);
Use this in view:
<div class="pagination">
{{ $documents->render() }} or {{ $documents->links() }}
</div>
i cant display posts' category with manyToMany relationship. i build relationship but can't display it.
// here is my post model
public function getCategory(){
return $this->belongsToMany(Category::class,
'post_categories','id','post_id');
}
// here is my controller
public function Allindex(){
$posts=Post::all();
return view('allposts',compact('posts'));
}
//here is my allposts blade
<div class="media-body">
<h4 class="media-heading">{{$post->pivot['name']}}</h4>
{{$post->created_at}}
</div>
You are passing the wrong param in a model relationship.
public function getCategory(){
return $this->belongsToMany(Category::class,'post_categories','post_id','category_id');
}
Now you can access it as below.
#foreach($post->getCategory as $category)
{{ $category->name }}
#endforeach
Or
{{ $post->getCategory->pluck('name')->implode(',') }}
Laravel is not automatically loading your relationship, the keyword you are looking for is eager loading.
You can eager load relationships with the with method like so:
$posts = Post::with('getCategory')->get();
I have a laravel query as the one below:
$campaign = Campaign::with(array('tracks.flights' => function($q) use ($dates)
{
$q->whereRaw("flights.start_date BETWEEN '". $dates['start']."' AND '".$dates['end']."'")->orderBy('start_date')
->with('asset')
->with('comments');
}
))
->with('tracks.group')
->with('tracks.media')
->orderBy('created_at', 'desc')
->find($id);
I am very new to laravel and right now the response from this query returns all the data required including the comments with the comments attributes from the DB.
What i want to achieve is manipulate the comments object so it includes the user name from the users table as the comments table has the user_id only as an attribute.
How can I achieve this? I am very new to laravel.
Your Comment model must have a relationship with the User model, such as:
public function user()
{
return $this->belongsTo('App\User');
}
Now when you are iterating comments you are able to do $comment->user->name or in your case it will be something like this:
#if(!$campaign->tracks->flights->isEmpty())
#foreach($campaign->tracks->flights as $flight)
#if(!$flight->comments->isEmpty())
#foreach($flight->comments as $comment)
{!! $comment->user->name !!}
#endforeach
#endif
#endforeach
#endif
Once you can do this, next step is to understand eager load with eloquent.
Hi I'm trying to query three tables from my client controller, a quick overview of my database, users clients projects tasks a user hasMany clients, projects and tasks and these projects and tasks also belongTo a client.
So I'm in the Client Controller and I want to query the logged in users clients projects, however when I try to do this I get thrown an undefined method error:
BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::user()
I'm not sure why this is occurring, I queried the clients projects separately and it works fine but when I add an additional layer it throws me the above error.
I'm a newbie on Laravel 4 so would appreciate some guidance to help rectify the error and help me understand where I'm going wrong.
My code is below:
ClientController.php
public function show($id)
{
$client = Client::find($id);
$client->load(array('projects' => function($query)
{
// With the clients for each project
$query->with('user');
}));
// Create an empty array
$associated = array();
// Loop through client projects
foreach($client->projects as $project):
// Loop through project users
foreach($project->user as $user):
// Check if the user is the same as the logged in user
if($user->id == Auth::user()->id){
// If yes add the $project to the $associated array
array_push($associated, $project);
}
endforeach;
endforeach;
// show the view
return View::make('clients.show')
->with('client', $client);
}
clients/show.blade.php
<?php $clients = $client->projects; ?>
#if (Auth::check())
#if (count($clients) > 0)
#foreach ($clients as $project)
<div class="one-third column">
<div class="projects">
<ul class="data">
<li><label>Project Name: </label><a class="btn btn-small btn-success" href="{{ URL::to('project/' . $project->id.'/show' ) }}"> {{ $project->project_name }}</a></li>
<li><label class="titletoggle">Project Brief <p>(click to toggle)</p></label><p class="brief">{{ $project->project_brief }}</p></li>
</ul>
<ul class='buttonslist'>
<li><button>Edit Project</button></li>
<li><button>Create Task</button></li>
<li><button>View Tasks</button></li>
</ul>
</div>
</div>
#endforeach
#else
<h3>You have no projects click here to create a project</h3>
#endif
#endif
The problem has to do with the way you are eager loading. Specifically this part.
$client->load(array('projects' => function($query)
{
// With the clients for each project
$query->with('user');
}));
The proper way to eager load these nested relationships would be.
$client->load(array(
'projects',
'projects.user',
));
Or more simply.
$client->load('projects.user');
Or you can set up the eager loading during the initial query.
$client = Client::with('projects.user')->find($id);
You also didn't mention that projects belongs to user. This relationship will need to be defined in the Project model.
class Project extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
The lack of this method is probably the cause of the error message. Eloquent will forward calls to undefined methods to it's internal query builder object. The query builder object doesn't have a user() method, so that's why you get that error.