laravel where condition with the % operator - laravel

today i'm trying to get modulus operator in where laravel clause but i can't get it work and after make some google researches and read the documentation i post this may be some one can help me to get a solution
Produit::has('caracteristique.image', '=', 2)
//->where('(caracteristique.image.caracteristique_id)', '=', 2)
->has('caracteristique.stock')
->where('market_id', Auth()->user()->market->id)
->where(DB::raw('count(`produits`.`caracteristiques`.`images`.`caracteristique_id`)'), '%2', [2])
->get();
i hope to select all products that have a relation with caracteristique table and for each caracteristique i have to find 2 iamge that come from the images table
i make this also
->has('caracteristique.image', '=', 2)
but the problem is this code is not ensure for each caracteristique like i can get 1 product with 2 caracteristique and 3 image but i suppose to have 4 image hope you understand my problem and hope you will help me to find a solution. thank you
here we have the models
produit
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\{
Market,
Caracteristique,
};
class Produit extends Model
{
protected $fillable = ['nom', 'detail', 'rabais', 'statut','demande', 'market_id'];
public function caracteristique()
{
return $this->hasMany(Caracteristique::class);
}
public function market()
{
return $this->belongsTo(Market::class);
}
}
caracteristique
namespace App\Models;
use App\Models\{
Produit,
Stock,
Image,
};
use Illuminate\Database\Eloquent\Model;
class Caracteristique extends Model
{
protected $fillable = [
'produit_id', 'couleur', 'prix', 'quantite', 'size', 'disponible',
];
public function produit()
{
return $this->belongsTo(Produit::class);
}
public function stock()
{
return $this->hasOne(Stock::class);
}
public function image()
{
return $this->hasMany(Image::class);
}
}
image
namespace App\Models;
use App\Models\{
User,
Caracteristique,
};
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $fillable = [
'caracteristique_id', 'user_id', 'nom'
];
public function caracteristique()
{
return $this->belongsTo(Caracteristique::class);
}
public function user(){
return $this->belongsTo(User::class);
}
}
produit can have multiple caracterisque but for each caracteristique it must have 2 images how i can i write this query to avoid query caractristique with one or doesn't have any image please?
and it just say error sql at line one after the %2 condition.

Related

Did anyone find solution for Trying to get property 'pivot' of non-object?

I tried to make relation btw organizer and user using pivot table, but when i tried to display the output its showing me error.
OrganizeController.php
public function show($id)
{
$organize = Organize::find($id);
return dd($organize->pivot->name);
}
Organize.php
namespace App;
use App\OrganizeUser;
use Illuminate\Database\Eloquent\Model;
class Organize extends Model
{
public function user()
{
return $this->belongsToMany(User::class)->withPivot('user_id');
}
}
Try this:
public function show($id){
$organize = Organize::find($id);
$organize->user()->get();
}

Laravel 6.2 whereHasMorph not returning expected value

Need some help for laravel polymorphic. I'm trying to filter from the main table which is Comment to get its morph tables and search for the key word FOO. I have tried with whereHas but get error saying to use whereHasMorph so I tried changing whereHas to whereHasMorph. But every time i filter, the result will be an empty collection even if the value exist in the table. So I went through laravel documentation and found the below sample. I tried the sample but I'm still getting an empty collection. I have tried reading but could not find a fix.
Below is the sample code which I have tried
** Models **
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $table = 'comments';
protected $fillable = [
'body',
'commentable_id',
'commentable_type'
];
public function commentable()
{
return $this->morphTo();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $table = 'posts';
protected $fillable = [
'title',
'body'
];
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
protected $table = 'videos';
protected $fillable = [
'title',
'url'
];
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
** Controller **
public function commentList () {
$comments = App\Comment::whereHasMorph(
'commentable',
['App\Post', 'App\Video'],
function ($query) {
$query->where('title', 'like', '%foo%');
}
)->get();
dd($comments);
}
Is there anything I'm missing out or do i need to configure something or install some packages ?
"php": "^7.2",
"laravel/framework": "^6.2",
Images of DB table
I have tried and dont know why suddenly work when i remove the first \ in the DB.
Changed \App\Post in the table to App\Post and also in the code then it work already. Thanks for all the help.

Cannot establish relationship between two tables in Laravel

I want to create a relation between lising and attribute table in laravel for that i have used following code to establish relationship between them but the data in my view is not coming from both the tables. I'm getting following error:
Call to undefined relationship [adListAttributes] on model
[App\Models\AdListing].
Here listing can have as many attribute associated with and attributes
can be associated to many listings
ad_listings:
id
title
name
date
ad_list_attributes table :
id
listing_id
name
namespace App\Models;
use Eloquent;
use Illuminate\Database\Eloquent\Model;
class AdListAttribute extends Model
{
protected $table = "ad_list_attributes";
public function Listings()
{
return $this->belongsToMany('AdListing', 'id', 'listing_id');
}
}
namespace App\Models;
use Eloquent;
use Illuminate\Database\Eloquent\Model;
class AdListing extends Model
{
protected $table = "ad_listings";
public function Attributes()
{
return $this->belongsToMany('AdListAttribute', 'listing_id', 'id');
}
}
Problem is that you are using belongsToMany in both the models.This will cause a problem.
In AdListAttribute model,
public function listing_information()
{
return $this->belongsTo('App\AdListing', 'id', 'listing_id');
}
In AdListing model,
public function adlisting_attributes()
{
return $this->hasMany('App\AdListAttribute', 'listing_id', 'id');
}
You can get the results using,
$response = AdListing::get();
if($response->adlisting_attributes)
{
foreach($response->adlisting_attributes as $attribute)
{
echo $attribute->name;
}
}
Problem is that ur not calling the relationship with the right name i assume
$listings = AdListing::with('Attributes')->get();
Update :
Try this :
use App\Models\AdListAttribute;
//
return $this->belongsToMany(AdListAttribute::class, 'listing_id', 'id');
Same for other model, then try

Argument 1 passed to App\Http\Controllers\ApiController::showAll() must be an instance of Illuminate\Database\Eloquent\Collection

I want to to retrieve all buyers for a specific saller.When I remove pluck and others methods chaining after get method it's working. But is not exact thing that I want. How Can I solve this provlem?
(source: licdn.com)
<?php
namespace App\Http\Controllers\Seller;
use App\Http\Controllers\ApiController;
use App\Seller;
use Illuminate\Http\Request;
class SellerBuyerController extends ApiController
{
public function index(Seller $seller)
{
$buyers = $seller->products()
->whereHas('transactions')
->with('transactions.buyer')
->get()->pluck('transactions')
->collapse()->pluck('buyer')
->unique('id')
->values();
return $this->showAll($buyers);
}
protected function showAll(Collection $collection, $code = 200)
{
return $this->successResponse($collection, $code);
}
protected function successResponse($data, $code)
{
return response()->json($data, $code);
}
}
Seller model hasMany relation to products
<?php
namespace App;
use App\Scopes\SellerScope;
class Seller extends User
{
public function products()
{
return $this->hasMany(Product::class);
}
}
Product Model hasMany relation to transactions
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
use SoftDeletes;
protected $fillable = [
'name', 'description', 'quantity', 'status', 'image', 'seller_id',
];
public function transactions()
{
return $this->hasMany(Transaction::class);
}
}
Transaction Model and relation to buyer
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Transaction extends Model
{
use SoftDeletes;
protected $fillable = [
'quantity', 'buyer_id', 'product_id'
];
public function buyer()
{
return $this->belongsTo(Buyer::class);
}
}
You are missing an import at the top:
use Illuminate\Support\Collection;
otherwise it assumes Illuminate\Database\Eloquent\Collection is to be used.
And values() obviously returns the support collection, not an eloquent one.
If you have a relationship from Buyer to Transaction you can go at this from the other direction to get buyers. You also need to make sure there is a relationship from Product to Seller (make sure you have the inverse of every relationship setup)
Buyer::whereHas('transactions.products.seller', function ($query) use ($seller) {
$query->where('id', $seller->id); // might have to be a more specific key name
})->get();
You would end up with a Eloquent Collection of Buyers who have transactions including products from a particular seller.
you should put use Illuminate\Support\Collection; in folder traits over apiResponser.php

Eloquent query: Filtering results based on relationship with other model

So I have two tables: posts and categories. It is a many-to-many relationship: A post has many categories and a category belongs to many posts.
Here are the models that describe their relationships:
Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name', 'slug'];
public function posts()
{
return $this->belongsToMany('App\Post');
}
}
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category');
}
}
I want to get all posts that have a particular category: tips
Here is what I tried:
$tips = Category::where('name', 'Tips')->posts()->with('categories')->take(4)->get();
I don't know the level of ignorance in this query, but I honestly expected it to work. It didn't.
I'll appreciate any help.
It's really as simple as:
$tipsCategory = Category::where('name', 'tips')
->get();
$posts = $tipsCategory->posts;

Resources