I'm trying to retrieve data using relationship in laravel and I'm getting this error all the time.
Column not found: 1054 Unknown column 'orders.customers_id'
in 'where clause' (SQL: select * from orders where
orders.customers_id in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
before this I was using these codes:
$data = DB::table('customers')
->join('orders', 'orders.customer_id', 'customers.id')
->get();
// convert to json string
$data = json_decode(json_encode($data), true);
return $data;
and it return the exactly result I want which is:
Here is my customers table:
orders table
Orders
class Orders extends Model
{
public function customer(){
return $this->belongsTo(Customers::class);
}
}
Customers
class Customers extends Model
{
public function order(){
return $this->hasMany(Orders::class);
}
DataController
class DataController extends Controller
{
public function all()
{
$All = Customers::with('order','order.customer_id')->paginate(10);
return response()->json([
'code' => 0,
'success' => true,
'data' => $All,
'pagination' => [
'current_page' => $All->currentPage(),
'last_page' => $All->lastPage(),
'prev_page_url' => $All->previousPageUrl(),
'next_page_url' => $All->nextPageUrl(),
'per_page' => $All->perPage(),
'total' => $All->total(),
'count' => $All->count(),
]
], 200);
Could you try this one
Order Model
class Orders extends Model
{
public function customer(){
return $this->belongsTo(Customers::class, 'customer_id');
}
}
DataController
class DataController extends Controller
{
public function all()
{
$All = Customers::order()->paginate(10);
return response()->json([
'code' => 0,
'success' => true,
'data' => $All,
'pagination' => [
'current_page' => $All->currentPage(),
'last_page' => $All->lastPage(),
'prev_page_url' => $All->previousPageUrl(),
'next_page_url' => $All->nextPageUrl(),
'per_page' => $All->perPage(),
'total' => $All->total(),
'count' => $All->count(),
]
], 200);
I just put the foreign key in the model already. I'm not entirely sure if its supposed to be in Order Model or in Customer Model either way try those both.
Hope it helps!
Remove order.customer_id from Customers::with('order','order.customer_id')->paginate(10);
So it should be
Customers::with('orders')->paginate(10);
Also as a customer can have many orders it is best to name your relation as orders
class Customers extends Model
{
public function orders()
{
return $this->hasMany(Orders::class);
}
}
In my controller
$All = Customers::with('order')->paginate(10);
return response()->json([
'code' => 0,
'success' => true,
'data' =>$All
], 200);
Customers model
class Customers extends Model
{
public function order(){
return $this->hasMany(Orders::class,'customer_id','id');
}
}
Orders model
class Orders extends Model
{
public function customers(){
return $this->belongsTo(Customers::class,'customer_id','id');
}
}
and its works.
But there is still got one thing which I'm not understand. Its works either I define the relationship in Customers model or Orders model or define the relationship in both
Related
I'm trying to get a partner for each order using Laravel resource collections. But this throws up an error:
Property [name] does not exist on this collection instance
I get partners this way
Order_product.php
//...
class Order_product extends Model
{
protected $fillable = ['order_id', 'product_id', 'quantity', 'price'];
public function partner()
{
return $this->hasManyThrough(
'App\Partner', 'App\Order',
'partner_id', 'id', 'order_id');
//orders partners order_products
}
//...
Resources\Order_product.php
class Order_product extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'order_id' => $this->order_id,
'product_id' => $this->product_id,
'quantity' => $this->quantity,
'price' => $this->price,
'status' => $this->order->status,
'product_name' => $this->prod->name,
//return error
'partner_name' => $this->partner->name,
];
/*
//this method return:
//Invalid argument supplied for foreach() {"exception":"[object]...
$partners = [];
foreach($this->collection as $partner) {
array_push($partners, [
// 'partner_name' => $this->partner->name
]);
}
return $partners;
*/
}
}
Each order has one partner name. In the future, I will group them, but now I just need to output the partner_name
is relations when you use hasMany or hasManyThrough it returns you a collection, so you should use it in foreach or use with index
return [
'product_name' => $this->prod->first()->name, //first array in collection using first()
];
OR
return [
'product_name' => $this->prod[0]->name, //first array in collection using index
];
or you can write this code in foreach!
As you are using hasManyThrough or hasMany laravel relationship returns Illuminate\Database\Eloquent\Collection Instance.
If you want to get name you have to have one Model instance.
Solution 1: $this->parthner->first()->name
Solution 2: See this hasOneThough
public function partner(){
return $this->hasOneThrough(
'App\Partner', 'App\Order',
'partner_id', 'id', 'order_id');
}
Depens on your app logic
Hope this helps you
I am trying to return pivot data to a resource.
The pivot table works, I can add and remove entrys like expected, but I am not able to get the user_id returned in ActivityResource...
In the Laravel Documentation it looks so easy, am I missing something?
// Activity.php
class Activity extends Model
{
public function members()
{
return $this->belongsToMany('App\User', 'activity_user', 'user_id', 'activity_id')->withPivot('activity_id','user_id')->withTimestamps();
}
}
// User.php
class User extends Authenticatable
{
public function joinedactivities()
{
return $this->belongsToMany('App\Activity');
}
}
In my ActivityController I want to return a newly created ActivityResource with 'eager-loaded' relationship
// ActivityController
public function show($id)
{
$activity = Activity::with('members')->findOrFail($id);
/* foreach ($activity->members as $user) {
echo $user->id . " "; // With this I can actually see the right ids
}
return;*/
return new ActivityResource($activity);
}
ActivityResource:
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'attendees' => $this->whenPivotLoaded('activity_user', function () {
return $this->pivot->user_id;
}),
];
}
I dont get any errors instead the attendees field is just not returned. I tried so many things, struggeling with that. Help very appreciated.
I am using Laravel 6.
->withPivot('activity_id','user_id') is not needed. Those fields will appear on your relation object no matter what. For the resource, I think you can do the following:
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
// If the relation 'members' is loaded, get an array of user ids otherwise, return null
'attendees' => $this->relationLoaded('members') ? $this->members->pluck('pivot.user_id')->unique()->all() : null
];
}
The main problem is the relationship is a Many to Many, meaning there's more than 1 pivot. With this solution, your object will look like this.
{
id: 3,
title: 'A Title',
attendees: [
1,
2,
3,
],
}
If you want the ids concatenated in a single string like in your commented foreach, replace all() by join(' ')
// If the relation 'members' is loaded, get an string of user ids otherwise, return null
'attendees' => $this->relationLoaded('members') ? $this->members->pluck('pivot.user_id')->unique()->join(' ') : null
{
id: 3,
title: 'A Title',
attendees: '1 2 3',
}
I have posts and users to which I assign one or more roles. The roles are the same between users and posts so that I can compare them.
To assign roles, I use the spatie/laravel-permissions library.
I'm building an API. I use Laravel's resources.
I would like to be able to make posts with the "admin" role visible only to users who have the "admin" role.
Currently, I can't.
CategoryController
$categorie = Category::where('slug->' . auth()->user()->lang, $slug)
->with(['posts' => function ($query) {
$query->published();
}])->first();
return new CategoryResource($categorie);
My PostResource
class PostResource extends JsonResource
{
public function toArray($request)
{
return [
'title' => $this->title,
'slug' => $this->slug,
'content' => $this->content,
'image' => $this->image,
'status' => $this->status,
'date' => $this->date,
'roles' => $this->roles
];
}
}
My CategoryResource
class CategoryResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'posts' => PostResource::collection($this->whenLoaded('posts')),
'count_posts' => $this->posts->where('status', 'published')->count(),
//'has_new_posts' => $this->has_new_post
];
}
}
Is it possible to manage this directly via resources? Or via a eloquent query
Personally, I had tried a different approach in the controller but I do not find it good and in addition I have an array instead of an object, which does not suit me.
$categorie = Category::where('slug->' . auth()->user()->lang, $slug)
->with(['posts' => function ($query) {
$query->published();
}])->first();
$posts = $categorie->posts;
$postsWithGoodRole = [];
foreach ($posts as $post) {
if ($post->hasAnyRole(Auth::user()->roles)) {
array_push($postsWithGoodRole, $post);
}
}
return response()->json([
'posts' => $posts
]);
I was using these codes in my controller to get all the data from my 2 tables and it works fine
$All = Customers::with('order')->paginate(10);
return response()->json([
'code' => 0,
'success' => true,
'data' => $All
], 200);
Here is how I define the relationship between these 2 tables
class Customers extends Model
{
public function order()
{
return $this->hasMany(Orders::class, 'customer_id', 'id');
}
}
class Orders extends Model
{
public function customers()
{
return $this->belongsTo(Customers::class, 'customer_id', 'id');
}
}
Now my desire output is to hide the order id, order timestamps and change the customer_id to customer's name (the customer's name is not in my orders db table).
I'm using 'data' => DataResource::collection($All) in my controller and this is my DataResource
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'order' => $this->order
];
}
and of course the output is same with the image above.
My database structure:
orders table:
customer table:
Can anyone help me with that?
The answer is simple and basically a copy of the official documentation. You simply need to wrap your orders in an OrderResource as well.
// DataResource
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'order' => OrderResource::collection($this->order)
];
}
// OrderResource
public function toArray($request)
{
return [
'items' => $this->items,
'quantity' => $this->quantity
];
}
I don't really understand why you would want to include the customer_name in your orders when it is already present on the customers object one hierarchy above. But if you really want to add it, you should be able to do so with: 'customer_name' => $this->customers->name.
As a side note: you really should be more consistent with your naming. Why is the resource called DataResource when it is about Customers? Why is your model called Customers in plural form rather than Customer in singular, which is the convention (and more logical if you consider that one model represents one customer). Why is your belongsTo relation called customers() in plural when it returns one customer, while your hasMany relation is called order whereas it returns one or more orders?
I created a table in relation to two other tables, however I do not know how to relate them when creating new projects, how could I do that?
I'll create the categories in a separated page in my admin, and when the user create's a new project he will be able to select an array of categories coming from the table.
My question is, how can I store the relation when POST the data? I've never done this before.
Project model
class Project extends Model
{
protected $table = 'projects';
protected $fillable = [
'name',
'slug',
'header',
'desc',
'about',
'url',
'status'
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function category()
{
return $this->belongsToMany(Category::class);
}
public function categories()
{
return $this->hasMany(Category::class);
}
}
Category model
class Category extends Model
{
protected $table = 'categories';
protected $fillable = [
'name',
'status'
];
public function subCategory()
{
return $this->hasMany(SubCategory::class);
}
public function projects()
{
return $this->belongsToMany(Project::class);
}
}
My actual Post create
public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
//Array
$categories = $request->categories;
$customer->projects()->create([
'name' => $request->name,
'header' => $request->header,
'desc' => $request->desc,
'about' => $request->about,
'url' => $request->url,
]);
//How do I store the relation?
return redirect('admin/clientes/editar/' . $customer->id);
}
use attach or sync for many to many relationships
reference : https://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models
public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
//Array
$categories = $request->categories;
$projects = $customer->projects()->create([
'name' => $request->name,
'header' => $request->header,
'desc' => $request->desc,
'about' => $request->about,
'url' => $request->url,
]);
//suppose here the $categories is an array of ids or a single integer variable id of the category that are related
$projects->categories()->attach($categories);
return redirect('admin/clientes/editar/' . $customer->id);
}
First: Make sure you have the following table in the database in order for the many to many relationship to work
table name : category_project
columns: category_id, project_id
Second: class Project needs only the following function for categories relation to work:
public function categories()
{
return $this->belongsToMany(Category::class);
}
Third: class Category needs only the following function for projects relation to work:
public function projects()
{
return $this->belongsToMany(Project::class);
}
Fourth: postCreate function should be as follow:
public function postCreate(ProjectCreateRequest $request, Customer $customer)
{
//should be an array of categories ids
$categories = $request->categories;
$project = $customer->projects()->create([
'name' => $request->name,
'header' => $request->header,
'desc' => $request->desc,
'about' => $request->about,
'url' => $request->url,
]);
//How do I store the relation? you can use attach or sync
$project->categories()->sync($categories);
return redirect('admin/clientes/editar/' . $customer->id);
}