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);
}
Related
i have manytomany relation ship between categroy and product
category model
class Attribute extends Model implements Auditable
{
use HasFactory, AuditableTrait;
protected $fillable = ['category','sub_categ'];
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
}
}
product model
class Product extends Model implements Auditable
{
use HasFactory, AuditableTrait;
protected $table = 'products';
protected $fillable = ['name','price','description', 'details'];
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
}
}
the pivot table
Schema::create('attributes_products', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('product_id')->constrained('products')->onUpdata('cascade')->onDelete('cascade');
$table->foreignId('attribute_id')->constrained('attributes')->onUpdata('cascade')->onDelete('cascade');
});
what should i do after this i did not undrestant how attach will work in pivot table and return it with the product as json response ?
edit
this is the schema i am working on
i want to give each product it's own category
and this is my product controller store function
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required|numeric',
'description' => 'required',
'details' => 'required',
'stocks' => 'required|numeric',
//'discounts' => 'required|numeric'
]);
$product = Product::create($request->only('name','price','description', 'details'));
$product->stocks()->create([
'quantity' => $request->stocks,
'product_id' => $product->id
]);
$product->discounts()->create([
//'discount' => $request->discounts,
'product_id' => $product->id
]);
if($request->hasFile('images'))
{
foreach( $request->file('images') as $file)
{
$file->store('public/products');
$product->images()->create([
'product_id' => $product->id,
'file_path' => $file->hashName()
]);
}
}
$product->categories()->sync([
'product_id' => $product->id,
'attribute_id'=> 1
]);
}
In your product model check your relation.
public function categories()
{
return $this->belongsToMany(Category::class);
}
Also usually the pivot table needs to have only 2 Ids. So in your case only 2 columns: product_id & category_id.
Your table name by convention should be category_product, otherwise, you should specify it on the second parameter on the relationship.
Fix this too, you got a typo on update:
$table->foreignId('attribute_id')->constrained('attributes')->onUpdate('cascade')->onDelete('cascade');
And finally to attach:
$product = Product::find(1);
$product->categories()->attach($categoryId);
All is explained very well on documentation too: https://laravel.com/docs/8.x/eloquent-relationships
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'm kinda new to Laravel and I hope someone we'll be able to give me some help.
I apologize for my english
So I'm trying to develop an application with some friends to manage our food by sending alert when the peremption date is near.
I'm developing the API, the actual structure is this way:
A user,
A product,
A basket containing the user_id, the product_id and of course the peremption date.
So now when I make a call to get the User 'stock' on my API I wish I could get something like this:
{
'id' : 1,
'peremption_date': XX-XX-XX,
'product' : {
'id' : 3,
'name': bblablabala,
'brand' : blablabala
},
'id' : 2,
'peremption_date': XX-XX-XX,
'product' : {
'id' : 4,
'name': bblablabala,
'brand' : blablabala
},
}
So I took a look on resources and saw that if I define the right relations, this could do the stuff for my output.
I'll link you my actual class declarations and their resources:
User:
//user.php
class User extends Authenticatable
{
use Notifiable, HasApiTokens;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function baskets()
{
return $this->hasMany(Basket::class);
}
}
Product:
//Product.php
class Product extends Model
{
protected $table = 'products';
protected $fillable = ['code_barre', 'product_name', 'generic_name', 'brand', 'quantity'];
public function basket()
{
return $this->belongsToMany(Basket::class);
}
}
//productResource.php
class ProductResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'code_barre' => $this->code_barre,
'product_name' => $this->product_name,
'generic_name' => $this->generic_name,
'brand' => $this->brand,
'quantity' => $this->quantity,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
Basket:
//Basket.php
class Basket extends Model
{
protected $table = 'baskets';
protected $fillable = ['user_id', 'product_id', 'dlc_date'];
public function user()
{
return $this->belongsTo(User::class);
}
public function product()
{
return $this->hasOne(Product::class);
}
}
//BasketResource.php
class BasketResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'dlc_date' => (string) $this->dlc_date,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
'product' => $this->product
];
}
}
So when I try to store a new basket in my store method:
//BasketController.php
public function store(Request $request)
{
$this->product->storeProduct($request->input('code_barre'));
$att = DB::table('products')
->where('code_barre', '=', $request->input('code_barre'))
->first();
$basket = Basket::create([
'user_id' => $request->user()->id,
'product_id' => $att->id,
'dlc_date' => $request->input('dlc_date')
]);
return new BasketResource($basket);
}
I get the following error (this one)
saying than products.id_basket does not exist and its right, it's not supposed to exist. This is Basket who have a product_id. so I know this is coming from the relationship I declared but I can't figure how to do it right.
Can someone come and save me ???
Thanks a lot, I hope you understood me !
Wish you a good day
As I look at your Basket model, it seems you have to change your:
public function product()
{
return $this->hasOne(Product::class);
}
to:
public function product()
{
return $this->belongsTo(Product::class);
}
Because you have product_id in your baskets table. To use hasOne() relation, you will need to remove product_id from baskets table and add basket_id to products table, because hasOne() relation is something like hasMany(), only calling ->first() instead of ->get()
I have a model Foo, which has many Bars:
class Foo extends Model
{
public function bars()
{
return $this->hasMany('App\Bar');
}
}
class Bar extends Model
{
public function foo()
{
return $this->belongsTo('App\Foo');
}
}
When saving a new Foo, the request payload comes with an array of Bar ids. I want to save these at the same time. This works:
public function store(StoreFoo $request)
{
$foo = Foo::create($request->validated());
foreach ($request->barIds as $barId) {
$foo->bars()->create(['bar_id' => $barId]);
}
}
My question is: is there a way to do this without a loop? I've tried sync and attach but these aren't applicable in this case.
The only way I can think of that you can achieve this without writing a loop yourself is by using the saveMany method on the HasMany relation. You can create instances of your Bar model and pass them all as an array to the saveMany method and that will save all of them and return an array of the created entities in response.
$foo->bars()->saveMany([new Bar(['id' => 1]), new Bar(['id' => 2])]);
That being said, Laravel uses a loop to save these models one by one under the hood so it doesn't really do much different to what you're doing now.
Similarly, there's also a createMany method that you can use in the same way as saveMany but instead of providing newly created models, you can provide arrays of attributes instead.
migration table sample
Schema::create('logs', function(Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->default(0)->index();
$table->string('type', 10)->index(); // add, update, delete
$table->string('table', 50)->index();
$table->unsignedBigInteger('row');
$table->dateTime('created_at');
});
Schema::create('log_fields', function(Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('log_id')->index();
$table->string('field', 50)->index();
$table->longText('old');
$table->longText('new');
});
model Log.php file
class Log extends Model
{
const UPDATED_AT = null;
protected $fillable = [
'user_id',
'type',
'table',
'row'
];
public function logFields()
{
return $this->hasMany(LogField::class);
}
}
model LogField.php file
class LogField extends Model
{
public $timestamps = false;
protected $fillable = [
'field',
'old',
'new'
];
public function log()
{
return $this->belongsTo(Log::class);
}
}
boot function for another model for save change in database.
hook created, updating and deleting for answer your question
public static function boot()
{
parent::boot();
static::created(function($resorce) {
$_log = new Log;
$_log->create([
'user_id' => session('uid', 0),
'type' => 'add',
'table' => $resorce->getTable(),
'row' => $resorce->fresh()->toArray()['id']
]);
return true;
});
static::updating(function($resorce) {
$_log = new Log;
$log = $_log->create([
'user_id' => session('uid', 0),
'type' => 'update',
'table' => $resorce->getTable(),
'row' => $resorce->fresh()->toArray()['id']
]);
foreach($resorce->getDirty() as $field => $new) {
$log->logFields()->create([
'field' => $field,
'old' => $resorce->fresh()->toArray()[$field],
'new' => $new
]);
}
return true;
});
static::deleting(function($resorce) {
$_log = new Log;
$log = $_log->create([
'user_id' => session('uid', 0),
'type' => 'delete',
'table' => $resorce->getTable(),
'row' => $resorce->id,
]);
foreach($resorce->fresh()->toArray() as $field => $value) {
$log->logFields()->create([
'field' => $field,
'old' => '',
'new' => $value
]);
}
return true;
});
}
Hope I have helped you to understand this.
I am having a yii2 error : common\models\Book has no relation named "favorite".
When I try to add:
public function search($params) {
$query = Book::find();
$query->joinWith(['author', 'profile', 'favorite']);
In the book model I do have the public function:
public function getFavoritedIcon() {
if (isset($this->favorite)) {
return '<i class="glyphicon glyphicon-asterisk books-form"></i>';
} else {
return '';
}
}
And also this extra function to get the icon
public function getFavoritedIcon() {
if (isset($this->favorite)) {
return $icon;
} else {
return '';
}
}
And this works fine in the grid where I want to add sorting and filter:
[
'label' => 'Favorites',
'attribute' => 'favorite',
'value' => 'favoritedIcon',
'hAlign' => 'center',
'vAlign' => 'middle',
'format' => 'raw',
'width' => '50px',
],
I do some different things from another models I am using:
in the grid i get the value as an icon from the book model but i used this before.
the other thing is that the Favorite model has not the same name that the table but it work fine in the grid
abstract class Favorite extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'user_favorite';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_id', 'book_id'], 'required'],
[['user_id', 'book_id'], 'integer'],
[['selectedTime'], 'safe']
];
}
Any clues what I am doing wrong ?
======================================================
UPDATE after Pedro del Sol answer
There was some errors in the code but the main one was answered by Pedro, I do had a favorite function in the Book model but not favorites with multiple output.
So now it is working like that:
In the Book model
public function getFavorite() {
$userID = Yii::$app->user->identity->id;
return Favorite::find()->where(['user_id' => $userID, 'book_id' => $this->id])->one();
}
public function getFavorites() {
$userID = Yii::$app->user->identity->id;
return $this->hasMany(Favorite::className(), ['book_id' => 'id'], ['book_id' => $this->id]);
}
public function getFavoritedIcon() {
if (isset($this->favorite)) {
return '<i class="glyphicon glyphicon-asterisk books-form"></i>';
} else {
return '';
}
}
In the BookSearch model:
public function search($params) {
$query = Book::find();
$query->joinWith(['favorites']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'title',
'author_id',
'rights_owner_id',
'user_favorite.user_id',
]
]);
and the grid view :
[
'label' => 'Favorites',
'attribute' => 'user_favorite.user_id',
'value' => 'favoritedIcon',
'hAlign' => 'center',
'vAlign' => 'middle',
'format' => 'raw',
'width' => '50px',
],
Having a method to getFavoritedIcon() is not the same as declaring a relation to getFavorite()
I assume that in your Book model class you have the methods getAuthor() and getProfile() which will return queries linking a Book with an Author and a Profile. You'll need something similar with Favorite(s) but I suspect the multiplicities will be different.
I think to declare your relation you'll need something like
/**
* #return \yii\db\ActiveQuery
*/
public function getFavorites()
{
return $this->hasMany(Favorite::className(), ['book_id' => 'ID']);
}
if the relation between Books and Favorites is one to many (most likely) or
/**
* #return \yii\db\ActiveQuery
*/
public function getFavorite()
{
return $this->hasOne(Favorite::className(), ['book_id' => 'ID']);
}
if the relation is one to one.
You can then use joinWith() with either 'favorite' or 'favorites' depending on the multiplicities of your relation.