I'm trying to do a double where on my table, but even if the arguments are correct I cannot view any result:
Here is my Review model:
class Reviews extends Model
{
protected $fillable=['id','product_id', 'user_id', 'rating', 'comment',
'approved', 'spam'];
public function user()
{
return $this->belongsTo(User::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
And this is my controller, where I pass the values and make the query;
a DD on the values shows them correctly:
public function storereview(Request $request){
$uid=Auth::user()->id;
$productID = $request->id;
$matchThese = ['product_id' => $productID, 'user_id' => $uid];
$result=Reviews::where($matchThese);
dd($result);
The DD of result shows this:
https://ibb.co/fSzRGH
I made a screenshot cause I can't indent this here.
What am I doing wrong? I don't understand! Thank you.
You need to use get() to actually execute the query:
$result = Reviews::where($matchThese)->get();
You need to use get() to get the result.
Reviews::where($matchThese)->get();
Related
I have 2 model one is task and another is order. I have used here one to many relation. Here is my code
public function orders(){
return $this->hasMany(Order::where('service_id', null), 'offerid');
}
for task
public function task()
{
return $this->belongsTo(Task::class, 'id');
}
I have to fetch only those table who have service_id null. when i make like this then showing error Too few arguments to function Illuminate\Database\Eloquent\Builder.
$tasks = Task::where('sub_category', $id)->with('serimages', 'serSignleImg', 'user', 'orders')->get();
This is the query where i fetching all data. I have pass them through api
You have error in your relation method
public function orders(){
return $this->hasMany(Order::class, 'offerid')->whereNull('offerid');
}
or you can do like this
public function orders(){
return $this->hasMany(Order::class, 'offerid');
}
$tasks = Task::where(['sub_category', $id)->with('serimages', 'serSignleImg', 'user', 'orders'=>function($query){$query->whereNull('offerid')}])->get();
I have laravel's basic auth system, a custom model called SecurityQuestion and a pivot table called securityquestion_user
User
public function securityquestion_user() {
return $this->belongsToMany(SecurityQuestion::class, 'securityquestion_user', 'question_id', 'user_id')->withPivot('question_id', 'user_id', 'answer');
}
SomeController
First option
foreach(Auth::user()->securityquestion_user as $question) {
dd($question);
}
Error: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$pivot
Second Option
foreach(Auth::user()->securityquestion_user() as $question) {
dd($question);
}
Error: returns false
Ok guys I got it,
I modified the relation to this:
public function securityquestion_user() {
return $this->belongsToMany('App\SecurityQuestion', 'securityquestion_user', 'user_id', 'question_id')->withPivot('question_id', 'user_id', 'answer');
}
Basically I reversed the order and place 'user_id' parameter before 'question_id' parameter.
foreach(Auth::user()->securityquestion_user as $question) {
print '<pre>';
print_r($question->pivot->answer);
print '</pre>';
}
Thank you very much for you interest.
Have you added namespace for auth in controller?
Like:
use Auth;
If not imported then use this for it:
foreach(\Auth::user()->securityquestion_user as $question) {
dd($question);
}
And also correct your function in user model
public function securityquestion_user() {
return $this->belongsToMany('App\SecurityQuestion::class', 'securityquestion_user', 'question_id', 'user_id')->withPivot('question_id', 'user_id', 'answer');
}
because your first option is correct except it!!
Try by using hasMany instead of belongsToMany in user model
public function securityquestion_user() {
return $this->hasMany (SecurityQuestion::class, 'securityquestion_user', 'question_id', 'user_id')->withPivot('question_id', 'user_id', 'answer');
}
Try like this, withPivot function expects one parameter: array or string.
public function securityquestion_user() {
return $this
->belongsToMany(SecurityQuestion::class, 'securityquestion_user', 'question_id', 'user_id')
->withPivot(['question_id', 'user_id', 'answer']);
}
And 'question_id' and 'user_id' are not necessary to be in withPivot function, since they are the foreign keys.
I have two models, User and Post
User Model:
public function posts()
{
return $this->hasMany('App\Post');
}
Post Model:
public function user()
{
return $this->belongsTo('App\User');
}
In my controller I have a public function which has:
$users = User::orderBy('is_ban', 'desc')->paginate(10);
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
Which is working as expected.
I also have one column in users table `is_ban' It's of boolean type.
I am looking for a query which will return the following:
Only get post which has been made by the user which has is_ban=false
perhaps i haven't understood you, but i hope it will help. You can add it to your Post model
public function getBannedUsersPosts()
{
return self::whereIn('user_id', User::where('is_ban', 0)->pluck('id'))->get();
}
I have this function in the Controller:
/**
*
* Edit Registration
*
*/
public function edit(Registration $id)
{
$logs = Log::where('registration_id', $id->id)->users()->get();
dd($logs);
return view('registrations_edit', ['registration' => $id, 'log' => $logs]);
}
The documentation says I can call
Log::where('registration_id', $id->id)->users()->get();
when I define users() in the Model.
public function users(){
return $this->belongsTo('App\User', 'id', 'user_id');
}
but when I call users() in the Controller i always get
Call to undefined method Illuminate\Database\Query\Builder::users()
What am I doing wrong?
Kind regards
I think you can try this :
public function edit(Registration $id)
{
$logs = Log::with('users')->where('registration_id', $id->id)->get();
dd($logs);
return view('registrations_edit', ['registration' => $id, 'log' => $logs]);
}
Hope this work for you
If your relations write in right way, try this:
$logs = Log::where('registration_id', $id->id)->users->get();
Does it have to be the 'id' column in database table which works fine for
show($id), edit($id) method in controller?
I want to replace $id with the value of 'post_id' column in database table, but it throws error: ModelNotFoundException
How can I fix it?
sample code:
database table:
id(int), post_id(varchar32), post_title(varchar32), post_content(text)
routes:
Route::resource('posts', 'PostsController');
PostsController:
public function show($id)
{
return View::make('posts.show');
}
When I visit http://localhost/posts/1
it should return the view of post which has id 1 in the table.
What if I what to return the view based on post_id value in the table?
Does it have to replace the parameter in show() or ?
In your PostsController you need to access the Post model to get the data from the db.
like this:
//PostsController
public function show($id) {
$post = Post::where('post_id', $id)->first();
return View::make('posts.show', compact('post'));
}
you can also use find
public function show($id) {
$post = Post::find($id);
return View::make('posts.show', compact('post'));
}