I tried this approach but no luck, I keep getting an error.
Call to a member function delete() on a non-object
Post::find($id)->delete();
Relationships:
public function posts()
{
return $this->hasMany('Post');
}
public function user()
{
return $this->belongsTo('User');
}
Solved using this.
{{ Form::open(array('method'=>'DELETE', 'route'=>array('post.delete', $post->id))) }}
Related
I got stuck with one to one relation
Facade\Ignition\Exceptions\ViewException
Class 'App\App\User' not found (View: /var/www/html/project1/resources/views/pengajuan/datauser/index.blade.php)
http://app.web/pengajuan/datauser
my User Model
public function dataUser()
{
return $this->hasOne(App\DataUser::class);
}
my DataUser Model
public function User()
{
return $this->belongsTo(App\User::class);
}
my controller
public function index()
{
$datauser = DataUser::all();
return view('pengajuan.datauser.index', compact('datauser'));
}
and my index view
#foreach ($datauser as $d)
<td>{{$d->masa_penilaian}}</td>
<td>{{$d->user->nama}}</td>
<td>{{$d->nip}}</td>
<td>{{$d->nuptk}}</td>
#endforeach
Can Help Me?
Thanks
Try DataUser::class instead of App\DataUser::class and User::class instead of App\User::class?
I try to add new method for simplify and reusable code but I failed
User Model :
public function products()
{
return $this->hasMany('App\Product');
}
public function obsolate()
{
return $this->where('status', 'obsolate');
}
When I try to retrieve like auth()->user()->products()->obsolate() it gives some error like
BadMethodCallException: Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::obsolate()
But If I do like auth()->user()->products()->where('status', 'obsolate')->get() It works.
Please, correct me if I wrong...
UPDATE
Product Model :
public function user()
{
return $this->belongsTo('App\User');
}
public function scopeObsolate($query)
{
return $query->where('status', 'obsolate');
}
I do auth()->user()->products->obsolate()->get() and it works!
But, if I want to use constraint group to solve this problem it will return error
public function scopeObsolate(Builder $builder)
{
return $builder->where(function (Builder $query) {
return $query->where('status', 'obsolate');
});
}
Argument 1 passed to App\Product::App{closure}() must be an instance of App\Builder, instance of Illuminate\Database\Eloquent\Builder given
SOLVED
use Illuminate\Database\Eloquent\Builder
Because products() method return Illuminate\Database\Eloquent\Relations\HasMany object. When you applying where to it, laravel will set the constraints on the relation query(It means Product eloquent builder). So you need to defined the method in Product model.
And change the obsolate method to scope method:
// Product model:
public function scopeObsolate($query)
{
return $query->where('status', 'obsolate');
}
Update:
The second error occurs because the type-hint, laravel cannot find the Builder in model. You need to
use Illuminate\Database\Eloquent\Builder
I am not using Laravel, but:
Your ->where/->hasMany seems to return different (internal) object that does not contain your custom method.
To chain properly, return $this only:
class FooBar extends \Illuminate\Database\Eloquent
{
public function products()
{
$this->hasMany('App\Product');
return $this;
}
public function obsolate()
{
$this->where('status', 'obsolate');
return $this;
}
}
I have set up two models and made the relationship between them. I want to pass the attributes of the user as well as the user_detail.
I have used a similar code somewhere and it worked perfectly. But it is not working here.
//This is the function in "User.php" model.
public function user_detail(){
return $this->hasOne('App\Profile');
}
//This is the function in "Profile.php" model.
public function user(){
return $this->belongsTo('App\User');
}
//edit function in ProfileController
public function edit($id)
{
$user=User::find($id);
return view('profile.edit')->with('data',$user->user_detail);
}
When I click the edit button in the view, I expect the extract all the details from user table as well as from user_detail table.
I think you should edit your this code a little bit
public function edit($id)
{
$user=User::findOrFail($id);
return view('profile.edit')->with('data',$user);
}
And in your blade file (profile.edit), You can get all details from User and Profile Model.
{{ $data->id }}
{{ $data->user_detail->YOURPARAMETERS }}
The problem is with the relationship naming. Make it camelCase like,
//This is the function in "User.php" model.
public function userDetail(){
return $this->hasOne('App\Profile');
}
//edit function in ProfileController
public function edit($id)
{
$user=User::find($id);
return view('profile.edit')->with('data',$user->userDetail);
}
Reference: https://github.com/laravel/framework/issues/4307#issuecomment-42037712
try using where instead of find and then use with:
$user = User::where('id', $id)->with('user_detail')->first();
return view('profile.edit')->with('data', $user);
In your model:
public function user_detail(){
return $this->hasOne('App\Profile', 'student_no');
}
I am not able to paginate in laravel in this situation
return $this->hasMany('App\News','category_id')->orderBy('id','desc')->paginate(20);
but says error. in controller I have also tried
$byCategories=Category::findOrFail($id)->paginate(20);`
it also says error.
help me.
My Model is
class Category extends Model
{
public function news()
{
return $this->hasMany('App\News','category_id')->orderBy('id','desc');
}
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
public function user()
{
return $this->belongsTo('App\User');
}
public function getRouteKeyName()
{
return 'slug';
}
}
another model one is
class News extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function category()
{
return $this->belongsTo('App\Category');
}
}
my controller code is
public function byCategory($id)
{
$byCategories=Category::findOrFail($id);
return view('back-end/news/byCategory', compact('byCategories'));
}
Thank you so much.
Pagination is not done in the Model
it is to be done in the controller, For example remove (->paginate(20);) from here
public function newsMany()
{
return $this->belongsToMany('App\News')->paginate(20);
}
keep it only as
public function newsMany()
{
return $this->belongsToMany('App\News');
}
and call the pagination in controller when returning the view
$news=Category::findOrFail($id)->newsMany()->paginate(20);
return view('view name', compact('news'));
I'm trying to get the Username of author to display that near comments which they wrote, when displaying a comments on page. I've got an error "Trying to get property 'name' of non-object"
Controller:
public function index(Site $site)
{
$comments=Comments::where('siteId', $site->id)->get();
return view('admin.comments.show', compact('comments'));
}
View:
#foreach($comments as $comment)
{{$comment->user->name}}
#endforeach
User model:
public function comments()
{
return $this->hasMany(Comments::class);
}
Comment model:
public function comments()
{
return $this->belongsTo(User::class);
}
I want to use relations. Thank you for help! :)
Change the relationship in the Comment model to this:
public function user()
{
return $this->belongsTo(User::class);
}
That should work.
In user model:
public function comments()
{
return $this->hasMany(Comment::class); // change Comments to Comment here
}
In Comment model:
public function user()
{
return $this->belongsTo(User::class);
}