I keep on getting this error method with does not exist and I have no idea how to fix it. I saw people who posted similar question like this and it usually the fault of all() but I am not using it. Can anyone help?
controller:
public function getPerson(){
return view('show');
}
public function getInfo($id) {
$user_info1 = user_info1::where('user_id',$id)->get();
$data['data'] = DB::table('personal_infos')::with('userinfo1s')->get()->sortByDesc('upload_time'); //error come from this line
return view('test', compact('user_info1','data'));
}
the function sortByDesc must before the function get();
sortByDesc is in collection laravel.. use orderby instead
$data['data'] = PersonalInfos::with('userinfo1s')->orderby('upload_time')->get();
Related
In my Model I have the function
App\Akte.php
public function lvs() {
return $this->hasMany('App\lvs', 'lv_id');
}
In my Controller I call
public function index()
{
$aktes = \App\Akte::all();
return view('admin.akte.index', ['aktes' => $aktes]);
}
And i´d like to extend my collection $aktes with the lvs table. Can somebody explain how to do this?
So my result should be a collection in which every single element of "Akte" has its Many collections of lvs in it..
If you also want the relationship loaded, just use:
$aktes = \App\Akte::with('lvs')->get();
See that. May be usefull for your needs.
https://laravel.com/docs/5.7/eloquent-relationships
public function index()
{
$aktes = \App\Akte::->all()->where('lvl', 2);
return view('admin.akte.index', ['aktes' => $aktes]);
}
somthing like this ...
I am trying to find a row with condition and that is...
A user has many profile pictures but there is one picture that is is_main
So this is what I wrote
public function profile_picture()
{
return $this->hasMany('App\User_profile_picture');
}
public function active_picture()
{
return $this->profile_picture()->find($this->is_main);
}
Now when I access it through
$picture = Auth::user()->active_picture;
It says
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
What is that I have to do to make it work?
Your code should be
public function profile_picture()
{
return $this->hasMany('App\User_profile_picture');
}
You are missing the return statement
If you want to use a Model method as a property, it has to return a relationship. Otherwise you need to call it as a method with the () operator. Like explained here.
So the solution to your question would be:
$picture = Auth::user()->active_picture();
edit: TIL you can also set a custom eloquent accessor:
public function getActivePictureAttribute()
{
return $this->profile_picture()->find($this->is_main);
}
$picture = Auth::user()->active_picture;
Yeah, you have to write the get...Attribute in camelCase, and can then use the attribute in snake_case/kebab-case or camelCase. (See the eloquent $snakeAttributes boolean variable.)
I think you can try this:
public function profile_picture()
{
return $this->hasMany('App\User_profile_picture');
}
public function active_picture()
{
return $this->profile_picture()->find($this->is_main);
}
Hope this work for you !!!
You must use class:
public function profile_picture()
{
return $this->hasMany(App\User_profile_picture::class);
}
I've got a problem with model's method with parameters. I've got 2 Page and ContentTranslation. I want to get ContentTranslation based on language, so I've got following method in Page model:
public function contentTranslations($language) {
return $this->hasMany('App\ContentTranslation')->where('language', $language);
}
and then I've got a method in PageController for getting the pages based on language:
public function getPagesByLanguage($language)
{
$pages = Page::orderBy('position', 'asc')->get();
foreach ($pages as $page) {
$page->contentTranslations($language);
}
$return $pages;
}
I'm returning my results in JSON format (I'm using Laravel as an api). But this approach doesn't work. It throws me error:
Call to undefined method Illuminate\Database\Query\Builder::contentTranslations()
Some time ago, I wanted to return contentTranslations for all languages, so I haven't got a parameter in contentTranslations model method and everything worked fine after calling $page->contentTranslations; in foreach loop. Why I've got the trouble with passing parameter into method?
You should use eager loading to avoid N+1 query problem:
public function getPagesByLanguage($language)
{
return Page::with(['contentTranslations' => function($q) use ($language) {
$q->where('language', $language);
}])
->orderBy('position', 'asc')
->get();
}
Also change your relation to:
public function contentTranslations() {
return $this->hasMany('App\ContentTranslation');
}
I keep getting errors on this line of code and I am not sure what the problem is?
Here is the route:
Route::get('/samples', 'TestsController#index');
Here is the controller line that is causing the error:
public function index()
{
return $this->makeView('samples.create');
}
Here is the error: Method [makeView] does not exist.
try with
public function getIndex()
{
return View::make('samples.create');
}
It should be
public function index()
{
return $this->make::View('samples.create');
}
You forgot the :: between make and View.
Im not aware of a 'makrView' method, but this is how a view is traditionally rendered.
$templateVars = array('varOne' => $templateVariableOne, 'varTwo' => $templateVariableTwo);
return View::make('samples.create', $templateVars);
Assuming you're using Laravel 4, you don't need $this to make a View in a controller.
public function index()
{
return View::make('samples.create');
}
Also you got View and make the wrong way round.
Documentation here: http://laravel.com/docs/responses#views
I'm pretty new to Laravel and am struggeling with the following problem at the moment:
I want to create a User-Group-Management. So I want to add Users to a Group and add Rights to these Groups, to have an authorization. My aim is to have a User::hasRight('exampleRight') function, which i can easily call. For that I wanted to have a function chaining inside of the User-Class. I have this function to create a Connection to the UserGroup-Table (which connects an User-ID to a Group-ID):
public function role()
{
return $this->hasOne('UserGroup');
}
The next function shall return an Array of rights. My plan was to write something like
public function rights()
{
$rights = Groups::find($this->role()->groups_id)->rights;
return $rights;
}
The GroupsModel of course has this function to get the Rights:
public function rights()
{
return $this->hasMany('Rights');
}
But obviously $this->role()->groups_id doesn't give me the groups_id but instead throws the Error Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$groups_id. When i leave the ->groups_id out, and do add it instead in the controller like:
(Attention Controller, no model!)
public function getUserRole()
{
return User::find(10)->rights->groups_id;
}
it gives me the right answer. Can somebody tell me, whats the error? I don't really get whats wrong...
Thanks you in advance!
Regards
In this particular method you need to remove the parenthesis:
public function rights()
{
$rights = Groups::find($this->role->groups_id)->rights;
return $rights;
}
When you do role() you're actually telling Laravel to return the relation object, so you can do things like
$this->role()->where('x', '=', 'y')->get();
Why do you have an intermediary table between User and Group? Simply give the User a group_id. Here is how I have built this in the past.
User belongsTo Group
Group belongsToMany Right
// Inside User Model
public function can($name)
{
foreach ($this->group->rights as $right)
{
if ($right->name == $name)
{
return true;
}
}
return false;
}
// Elsewhere
if (!$user->can('edit_blogs'))
{
return 'FORBIDDEN';
}
Look into eager loading to mitigate performance issues.
Look into filters to apply these access restrictions at the controller or route level.