Laravel paginate (with foreign key) - laravel

I am trying to fetch some data out of a database with Laravel. The fetching part works fine, untill I want to use paginate for the data.
In my controller (RepairController) I use this function to get everyting (without pagination):
$pendingRepairs = Repair::get()->where('status.completed', 0);
This works fine. Untill I add the pagination function. Then it gives me an error:
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::paginate does not exist.
Some research on the internet shows me that I need to remove the ::get() part. But how? What is the alternative to use the get part, since I use the status.completed foreign key value?
My RepairController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repair;
class RepairController extends Controller
{
public function index()
{
$allRepairs = Repair::get();
$pendingRepairs = Repair::get()->where('status.completed', 0)->paginate(2);
$doneRepairs = $allRepairs->where('status.completed', 1);
return view('repair.index', [
'pendingRepairs' => $pendingRepairs, 'doneRepairs' => $doneRepairs
]);
}
....
}
In Repair.php I have a link to the Statuses:
public function status()
{
return $this->belongsTo(\App\Status::class);
}
Any ideas? Thanks :)

You can use whereHas() to add constraints on relationship.
$pendingRepairs = Repair::whereHas('status', function($query) {
$query->where('completed', 0);
})
->paginate();

There is many ways to do that. First thing is whereHas as Anurat answered.
There is also another way to just join table you need
In different situations they will work in different complexity.
Note, that whereHas will do subselect
$pendingRepairs = Repair::query()
->join('statuses', 'statuses.repair_id', 'repairs.id')
->where('statuses.completed', 0)
->select('repairs.*')
->paginate();
In case when your table names are: repairs statuses and you have repair_id foreign key, if not just replace them :)

Thanks for all the replies.
I think they all will work. But I've found out the use of datatables (datatables.net).
Since that will add the pagination itself, I don't need the Larvel once anymore.

Related

Trying to get shorter link laravel

This is my first question in this forum so please be patient to me ;)
I'm making a blog using Laravel and
I have a page with a view of all articles inside my database
enter image description here
Last column is "Action" column whet after hit the button you can see single article.
The problem is to show each content (title,subtitle etc.) i must create loooong UrL like this
http://127.0.0.1:8000/article/Test/Test/test/2020-05-08%2016:00:00
Is there any chance to cut URL to be like that:
http://127.0.0.1:800/article/Test
and still have all content?
Files
web.php
Route::get('article/{title_article}/{subtitle_created}/{text_article}/{created_at}','ReadController#index');
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ReadController extends Controller
{
public function index($title_article,$subtitle_article,$text_article,$created_at)
{
return view('article',compact('title_article','subtitle_article','text_article','created_at'))->with('title','Artykuł');
}
}
You should use the primary key (probably an incrementing id) of your database object (App\Article) to identify the object. Laravel has a wonderful function to automatically convert the primary key (id) in the route to an eloquent model.
In your case:
Route::get('article/{article}','ReadController#index');
and
public function index(Article $article) { ... }
I hope this helps you. If you don't understand parts of it, feel free to ask :)

Laravel6 WhereHas Error 500 when using AJAX

im new to Laravel and facing an interesting Issue right now in my App.
I have 3 tables.
Producers
id
producer_name
Types
id
type_name
Models
id
model_name
device_type_id
device_producer_id
Within my Producers Model I have defined the follwing Filter method:
public function scopeFilterByType($query, $type_id)
{
$query->whereHas('models', function($q) use $type_id { $q->where('device_type_id', $type_id );});
}
Using Tinker I can do the following:
App\DeviceProducer::filterByType(3)->get()
And get full response with my Producers associated to my given type.
I created an Function so when a user select a device type Ajax will load all Producers from this type.
public function reqProducer(Request $request)
{
$producers = DeviceProducer::filterByType($request->type_id)->get();
return response()->json( $producers );
}
But when AJAX is calling my endpoint it gets HTTP500 error.
I figured out when using a request without WhereHas for example:
$producers = DeviceProducer::where('id', $request->producer_id)->get();
It just works fine and I get my results. So it seems have to do something with "WhereHas". I know I could Solve this by first asking Models Table and the creating an Foreach loop. But I this solution would be less readable then my first attempt.
Does anyone has an suggestion what im doing wrong or is it just like there is noch AJAX support for WhereHas querys?
Kind regards
Mike
I think this is your issue use $type_id
Please fix as
public function scopeFilterByType($query, $type_id)
{
$query->whereHas('models', function($q) use ($type_id) { $q->where('device_type_id', $type_id );});
}

How to use multiple models in one controller

I have six models that I need to get in one instance in my controller. How can I do that?
I have my six models:
CommentaireCritique
CommentaireNews
CommentaireDossier
CommentaireEpisode
CommentaireSerie
CommentaireTrailer
They all have the same structure in my database, and I would like to show the latest comms on one single page. I don't know if it's possible to bind them in a single controller. I tried that, but it's not working.
public function index()
{
$comms = CommentaireCritique::all() && CommentaireNews::all()
&& CommentaireDossier::all() && CommentaireEpisode::all()
&& CommentaireSerie::all() && CommentaireTrailer::all()
->get();
return view('admin.commentaires.index', compact('comms'));
}
just after the namespace , before the class declaration
use yourAppNameSpace/modelName
There is no limits to the number of models you can instantiate in your controller as long as you declare them above correctly.I think what you need is way to merge the result of all the models if that is so, then you have to use the merge method, otherwise can you please clarify a little bit your question.
yes, you can retrieve them at one controller,
you're already halfway there, you should separate on different variable
public function index()
{
$comms = CommentaireCritique::all()
$news = CommentaireNews::all()
$dossier = CommentaireDossier::all()
$episodes = CommentaireEpisode::all()
$series = CommentaireSerie::all()
$trailers = CommentaireTrailer::all()
return view('admin.commentaires.index', compact('comms','news','dossier','episodes','series','trailers'));
}
if you want put them in one variable, you can use collection docs
All of the results from all() function returns laravel collections. So use concat() function to concatenate all those into one collection
public function index()
{
$coms = CommentaireCritique::all()
->concat(CommentaireNews::all())
->concat(CommentaireDossier::all())
->concat(CommentaireEpisode::all())
->concat(CommentaireSerie::all())
->concat(CommentaireTrailer::all());
return view('admin.commentaires.index', compact('comms'));
}

Laravel 5.5 - Deep Relations calling by -->

I now spent hours googling and experimenting on trying to get an relation with two intermediate tables working.
My database looks like this:
(apt_id is apartment_id in real, was shorter to write)
I have every relation one away setup correctly with belongsTo and and hasMany:
EXAMPLE FROM House.php
public function user()
{
return $this->belongsTo('App\User');
}
public function apartments()
{
return $this->hasMany('App\Apartment');
}
Isn't there a way to access these relations like:
$house->apartments->tenants->entries
in Blade:
#foreach ( $house->apartments->tenants->entries as $entry )
, since I want to display all house entries on house.show (Blade View)
The only way it's working is by using a bunch of foreach inside each others... :/ and they define the order...
Using my wanted relation calling produces:
Property [tenants] does not exist on this collection instance.
displayed on the page.
Greetings,
Pat
I don't think you can achieve what you want using the code you posted, because when calling, for example, $house->apartments it returns a Collection object. So, it is not dealing with database anymore, that's why you would need to use a bunch of #foreachs.
I don't know if this is the best way to solve this, or if it will help you in your actual problem, but you could think this problem backwards and try something like this:
$entries = \App\Entry::whereHas('tenants', function($q) use ($house) {
$q->whereHas('apartments', function($q1) use ($house) {
$q1->where('apartments.house_id', $house->id);
});
})->get();
And in the view:
#foreach ($entries as $entry)
{{ $entry->tenant->apartment->house->name }}
#endforeach

How to acces is_* property in laravel model?

I am working with laravel 4.2 and have table in db with property is_active.
When I try to access this model property:
$model->is_active
I am getting following error:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
So question is how to access this property?
Please do not recommend to rename this field in the database if possible because this is already existing database in production.
Here is my model class:
class Position extends \Eloquent {
protected $table = "hr_positions";
protected $fillable = ['slug', 'info_small', 'info_full', 'is_active', 'start_date', 'end_date', 'tags', 'user_create_id', 'user_update_id'];
use \MyApp\Core\StartEndDateTrait;
public function postulations(){
return $this->hasMany('Postulation', 'position_id', 'id');
}
}
Latest notice:
All this error ocurrs on a page where I am creating my entity. In the controller before forwarding to the page I am doing:
$position = new \Position();
and then, for example, following code produce error as well:
dd(($position->getAttribute('is_active')));
but if I replace $position = new \Position(); with
$position = \Position::first();
error is gone?
What is going on here?????
Laravel does a lot of magic behind the scenes, as in, calls a lot of php magic methods.
If a called property is not defined, __call is invoked which in Eloquent calls getAttribute().
Steps taken by getAttribute($key) are
Is there a database field by this key? If so, return it.
Is there a loaded relationship by this key? If so, return it.
Is there a camelCase method of this key? If so, return it. (is_active looks for isActive method)
Returns null.
The only time that exception is thrown is in step 3.
When you create a new instance, eloquent has no idea what kind of fields it has, so if you have a method by the same name, it will always throw a relation error, this seems to be the case in both Laravel4 and Laravel5.
How to avoid it? Use the getAttributeValue($key) method. It has no relation checks and returns null by default.
Alternatively you can also add a get mutator for your field.
I have found a hack for this. Still not ideal but at least I have some solution. Better any than none.
So This code produce problem:
$position = new \Position();
if($position->is_active){
//
}
and this one works fine, this is solution even hacky but solution:
$position = new \Position(['is_active' => 0]);
if($position->is_active){
//
}
I will wait if someone give better, cleaner solution. If no one comes in next few days I will accept mine.

Resources