In my setting model class i have this
public function user()
{
return $this->hasMany('App\User');
}
and in my user model class i have this
public function settings()
{
return $this->belongsTo('App\settings', 'id');
}
I tried both these queries, trying to get the user information but failed.
$data = DB::table('settings')
->where('id', '=', $id)
->get()
->toArray();
and
$table = \App\settings::where('id', '=', $id);
$query = $table->user()
->get()
->toArray();
I'm getting this error
Call to undefined method Illuminate\Database\Eloquent\Builder::user()
How do you do relation query? Sorry new to laravel here.
$table = \App\settings::where('id', '=', $id)->first();
$query = $table->user->toArray();
Eager load
$table = \App\settings::with('user')->where('id', '=', $id)->first();
$query = $table->user->toArray();
Related
i have this index function that will show data from two different tables:
public function index()
{
$complaints = DB::table('complaint')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->get();
return view('admin.complaints',compact('complaints'));
}
and in the next function i want to show a single row using the same thing above by 'id'
i tired this:
public function show($id)
{
$complaints = DB::table('complaint')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->where('id', $id)->first()
->get();
return $complaints;
}
but i'm getting this error
Call to undefined method stdClass::get()
For creating where statements, you can use get() and first() methods. The first() method will return only one record, while the get() method will return an array of records , so you should delete first() , so the code should be like that .
public function show($id)
{
$complaints = DB::table('complaint')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->where('id', $id)
->get();
return $complaints;
}
$users = User::with('departments')
->whereHas('departments', function ($q) use ($departments) {
return $q->whereIn('department_id', $departments);
})->whereNotNull('users.email')
->get();
public function users(){return $this->morphedByMany(User::class, 'departmentable');}
in department model
and
public function departments(){return $this->morphToMany(Department::class, 'departmentable');
}
in user model
in your whereIn statement you use department_id column in departments table, are you sure of this, isn't just 'id' ('departments.id')?
$users = User::with('departments')
->whereHas('departments', function ($q) use ($departments) {
return $q->whereIn('departments.id', $departments);
})->whereNotNull('users.email')
->get();
For example I can use:
$address = $user->address
which will return the addresses for the user.
// User.php (model)
public function address()
{
return $this->hasMany(Address::class, 'refer_id', 'id');
}
public function billingAddress()
{
return $this->address()
->where('type', '=', 1)
->where('refer_id', '=', $this->id)
->first();
}
However, I would like to return the BillingAddress for the user depending on this where clause. How do I do it?
EDIT:
If I use this inside... OrderController#index it returns correctly
$orders = Order::with('order_fulfillment', 'cart.product', 'user.address', 'payment')->get();
return new OrderResource($orders);
However, If I change it to:
$orders = Order::with('order_fulfillment', 'cart.product', 'user.billingAddress', 'payment')->get();
return new OrderResource($orders);
I get this error:
Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function addEagerConstraints() on null
One option is you can use whereHas in your query. For example,
$orders = Order::with('order_fulfillment', 'cart.product', 'user.address', 'payment')
->whereHas(
'address', function ($query) {
$query->where('type', '=', 1)
->first();
}
)->get();
return new OrderResource($orders);
This is one option. try to dd($orders) an find if its working.
You had an another option like this, in your model
public function address()
{
return $this->hasMany(Address::class, 'refer_id', 'id');
}
Add relations like
public function billingAddress()
{
return $this->hasOne(Address::class, 'refer_id', 'id')->where('type', 1);
}
And
public function shippingAddress()
{
return $this->hasOne(Address::class, 'refer_id', 'id')->where('type', 2);
}
Then in your query,
$orders = Order::with('order_fulfillment', 'cart.product', 'user.address','user.billingAddress', 'user.shippingAddress', 'payment')->get(); return new OrderResource($orders);
I am having trouble passing parameters from my Controller to my Model in Laravel 5.
My Model:
class Widget extends Model {
protected $fillable = array('type');
public function widget_fields_with_data($id)
{
return DB::table('widget_fields')
->join('banner_data', function($join) {
$join->on('banner_data.widget_field_id', '=', 'widget_fields.id')
->where('banner_data.banner_id', '=', $id);
})
->select('widget_fields.*', 'banner_data.value');
}}
In My Controller
$widget->widget_fields_with_data('54')->get();
This seems to return an Undefined variable: id error and I can't figure out why.
If i hardcode the value in the Model everything works okay.
Use use() statement:
function($join) use($id)
Here is your answer. You haven't passed the $id in inner function.
public function widget_fields_with_data($id)
{
return DB::table('widget_fields')
->join('banner_data', function($join) use($id) { // pass $id here
$join->on('banner_data.widget_field_id', '=', 'widget_fields.id')
->where('banner_data.banner_id', '=', $id);
})
->select('widget_fields.*', 'banner_data.value');
}}
Hello everyone I'm trying to make pagination in Laravel 4 but my code doesn't work.
I have controller with action:
public function getSingleProduct($prodName, $id)
{
$singleProduct = Product::getOne($id);
$getAllReviews = Review::getAllBelongsToProduct($id);
$this->layout->content = View::make('products.single')
->with('reviews', $getAllReviews)
->with('products', $singleProduct);
}
and I want to paginate getAllReviews (5 per page). I tried like this:
$getAllReviews = Review::getAllBelongsToProduct($id)->paginate(5); but it doesn't work for me. Here is also my Review model
public static function getAllBelongsToProduct($id) {
return self::where('product_id', '=', $id)
->join('products', 'reviews.product_id', '=', 'products.id')
->select('reviews.*', 'products.photo')
->orderBy('created_at', 'desc')
->get();
}
Where I have a mistake?
Instead of that static method on your model use query scope, this will be flexible:
// Review model
public function scopeForProduct($query, $id)
{
$query->where('product_id', $id);
}
public function scopeWithProductPhoto($query)
{
$query->join('products', 'reviews.product_id', '=', 'products.id')
->select('reviews.*', 'products.photo');
}
Then use it:
// get all
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->get();
// get paginated
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->paginate(5);
latest is built-in method for orderBy('created_at', 'desc').
If you want to have just a single call in your controller, then chain the above and wrap it in methods on your model.