Call to a member function where() on null Route Model Binding Laravel 7 - laravel

before so sorry for my bad english. Whats mean that error? "Call to a member function where() on null". In my code need obtain all application for a each category.
Im interesting in use a Model route binding, its works well when I insert a one model, for example: Application. Example:
Route::get(applications/{application},'ApplicationController#show')->name('apps'));
But when I try use 2 model in a route, have this problem. Here my code:
Route::get('categories/{category}/applications/{application:category_id}','ApplicationController#show')->name('apps');
My models:
class Application extends Model{
protected $fillable = ['name', 'price', 'category_id', 'vote', 'image_src'];
public function users()
{
return $this->belongsToMany(User::class, 'applications_users_states', 'application_id', 'user_id')
->withPivot('state_id')
->withTimestamps();
}
public function categories()
{
return $this->belongsTo(Category::class);
}
public function logs()
{
return $this->belongsToMany(Log::class)->withTimestamps();
}
}
class Category extends Model
{
protected $fillable = ['name', 'description'];
public function applications()
{
$this->hasMany(Application::class);
}
}
My Controller:
public function show(Category $category,Application $application)
{
return $application;
}
Thanks for your time and for your knowledge!!!!!

Related

Problem with posting distance relationship data of an authenticated user in laravel

I have three models as per the code below. I have a problem of posting client data who is a user within the system and should be authenticated.I am thing of something like this:
public function store_application(Request $request)
{
$data = new LoanApplication();
$data->client_id = $client->id;
$data->save();
}
Loan Application Model
class LoanApplication extends Model
{
public function client()
{
return $this->hasOne(Client::class, 'id', 'client_id');
}
}
Client Model
class Client extends Model
{
public function client_users()
{
return $this->hasMany(ClientUser::class, 'client_id', 'id');
}
}
Client User Model
class ClientUser extends Model
{
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
}
Any help will be highly appreciated
Here is the answer to the above question:
public function store_application(Request $request)
{
$user = Auth::user();
$client= ClientUser::where('user_id', $user->id)->pluck('client_id');
$data = new LoanApplication();
$data->$client;
$data->save();
}

How to display owner of a post in laravel admin list display?

public function index()
{
$query = Activity::orderBy('id','DESC')->with('provinces');
if(!Auth::user()->hasRole('Administer')){
$query=$query->where('province_id', Auth::user()->id);
}
$activities = $query->latest()->get();
return view('activity.index',compact('activities'));
}
How to display owner of a post in laravel admin list display?
My User Model
class User extends Authenticatable implements HasMedia
{
public function activities()
{
return $this->hasMany(Activity::class);
}
}
My Activity Model:
class Activity extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function provinces()
{
return $this->belongsToMany(Province::class);
}
}
My province Model
class Province extends Model
{
protected $fillable = [ 'title', 'post_id'];
public function activities()
{
return $this->belongsToMany(Activity::class);
}
public function user()
{
return $this->belongsToMany(User::class);
}
}
I want to show the content to the user by province.
And also the admin can see all the content.
please help me i am new in laravel
Based on your comment, you should change your province conditions to:
if (!Auth::user()->hasRole('Administer')) {
$query = $query->where('province_id', Auth::user()->province_id);
}
You have defined that each activity belongs to an user. So can access owner of each activity like this:
$activity->user->name;
And if your activity belongs to a province, you should define your relation in this way:
class Activity extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
public function province()
{
return $this->belongsTo(Province::class);
}
}
Also for your Province model:
class Province extends Model
{
protected $fillable = [ 'title', 'post_id'];
public function activities()
{
return $this->hasMany(Activity::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
And this is best practice to use eager loading for fetching related records. It will look like this one:
$query = Activity::with(['user', 'province'])->orderBy('id','DESC');
Also another hint: try to use better variable names. You can use $activities instead of $query.

Laravel 4.2 Eloquent relationship non-object result

I want show $post->category->cat_name but I have an error Trying to get property of non-object!
Model : Category
class Category extends \Eloquent {
protected $guarded = array();
public static $rules = array();
public function posts(){
return $this->hasMany('Post');
}
}
Model Post
class Post extends \Eloquent {
protected $guarded = array();
public static $rules = array();
public function category(){
return $this->belongsTo('Category');
}
public function user(){
return $this->belongsTo('User');
}
public function comments(){
return $this->hasMany('Comment');
}
}
PostsController
public function show($id)
{
$post = Post::find($id);
return View::make('posts.show',compact('post'));
}
}
In View $post->post_name works.
$post->user->username works
$post->comments->content works
but $post->category->cat_name doesn't works
And I can do CRUD with categories.
So I think there is a probleme with my model Category but i don't understand

Laravel5.3:How to use pluck in relationship for bindign Form::select element?

This is my model:
class Positions extends Model implements Repository
{
protected $fillable = ['index_id', 'title', 'description'];
public function index()
{
return $this->belongsTo('TEST\Indices', 'index_id');
}
public function getById($id)
{
return $this->with('index')->find($id);
}
}
how to use pluck() in getById() function for listing index relationship?
You can do it like this :
return $this->with('index')->find($id)->pluck('index.indexField');

Laravel 'boot' function is not firing

I have the following in an eloquent model
class Bucket extends \Eloquent {
protected $fillable = ['name'];
protected $appends = ['slug'];
public function __construct(){
$this->key = substr(str_shuffle(MD5(microtime())), 0, 24);
}
public static function boot(){
dd('check');
}
public function users(){
return $this->belongsToMany('User');
}
public function getSlugAttribute(){
return slugify($this->name);
}
}
However I'm able to read and update the model with no problem. I was under the impression boot was supposed to be called every time a model was instantiated, is that wrong?
Here's one of the controllers I use to view all the buckets
public function index()
{
return Auth::user()->buckets;
}
In your constructor, try calling
parent::__construct();

Resources