I have the following tables for my many to many relationship: soldhomestests, tasks and soldhomestest_task (as the pivot).
My soldhomestests table has already been populated with data. How do I get my soldhomestest_task pivot table to populate with data upon the creation of a new task that meets conditions in my soldhomestest table? In my example, I want to store the relationship data when the following conditions are met:
'tasks.city' = 'soldhomestests.city'
'tasks.address' = 'soldhomestests.address'
I can't seem to find any documentation on how to proceed with this?
MODELS:
class Task extends Model
{
protected $fillable = [
'address', 'city', 'state',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function soldhomestests()
{
return $this->belongsToMany('App\Soldhomestest');
}
}
class Soldhomestest extends Model
{
public function tasks()
{
return $this->belongsToMany('App\Task');
}
}
CONTROLLER:
public function store(Request $request)
{
$this->validate($request, [
'address' => 'required|max:255',
'city' => 'required|max:255',
'state' => 'required|max:255',
]);
$request->user()->tasks()->create([
'address' => $request->address,
'city' => $request->city,
'state' => $request->state,
]);
return redirect()->route('settings.index');
}
Don't believe this is the Laravel way but I modified my controller to create an array of IDs using the where condition:
public function store(Request $request)
{
$this->validate($request, [
'address' => 'required|max:255',
'city' => 'required|max:255',
'state' => 'required|max:255',
]);
$newtask = $request->user()->tasks()->create([
'address' => $request->address,
'city' => $request->city,
'state' => $request->state,
]);
$condition = DB::table('soldhomestests')->where([
['soldhomestests.address', '=', $request->address],
['soldhomestests.city', '=', $request->city],
])->pluck('id');
$lastid = $newtask->id;
$tasksoldhome = Task::find($lastid);
$tasksoldhome->soldhomestests()->sync($condition);
return redirect()->route('settings.index');
}
Using eloquent, you can do this way.
$task=new Task();
$task->city=$request->city;
$task->address=$request->address;
$task->save();
$soldhometests=Soldhometest::all();
foreach($soldhometests as $soldhometest)
{
if($task->city==$soldhometest->city && $task->address==$soldhometest->address)
{
$soldhometest_task=new SoldhometestTask(); // pivot model
$soldhometest_task->task_id=$task->id;
$soldhometest_task->soldhometest_id=$soldhometest->id;
$soldhometest_task->save();
}
}
Related
I'm just learning laravel. I want update key / value in database with laravel api but not work.
My products model is one to many with ProductMeta and many to many with contents model.
My Models
class Product extends Model
{
use HasFactory;
protected $guarded = [];
public function productMeta()
{
return $this->hasMany(ProductMeta::class);
}
public function content()
{
return $this->belongsToMany(Content::class, 'product_contents')->withTimestamps();
}
}
class ProductMeta extends Model
{
use HasFactory;
protected $guarded = [];
public function products()
{
return $this->belongsTo(Product::class);
}
}
class Content extends Model
{
use HasFactory;
protected $guarded= [];
public function product()
{
return $this->belongsToMany(Product::class, 'product_contents');
}
Controller
public function update(Request $request, $id)
{
$product = Product::findOrFail($id);
DB::table('product_metas')
->upsert(
[
[
'product_id' => $product->id,
'key' => 'name',
'value' => $request->name,
],
[
'product_id' => $product->id,
'key' => 'price',
'value' => $request->name,
],
[
'product_id' => $product->id,
'key' => 'amount',
'value' => $request->name,
],
],
['product_id','key'],
['value']
);
return \response()->json([], 204);
}
Table Structure
API parameter
I tried with update and updateOrcreate and updateOrInsert and upsert methods.
just in upsert method writed database but inserted new data.not updated.
In your case, you should use updateOrCreate() instead of upsert.
Product::updateOrCreate([
'product_id' => $id,
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount
]);
or
Product::upsert([
[
'product_id' => $id,
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount
]
], ['product_id'], ['name', 'price', 'amount']);
In addition your problem is your table name is not matching with your structure table name. In your controller DB::table('product_metas') should be DB::table('products_meta').
my problem solved this way:
ProductMeta::query()->where('product_id', $id)->upsert([
['product_id' => $id, 'key' => 'name', 'value' => $request->name],
['product_id' => $id, 'key' => 'price', 'value' => $request->price],
['product_id' => $id, 'key' => 'amount', 'value' => $request->amount]],
['product_id'], ['value']);
$contentRecord = Product::find($id);
$contentRecord->content()->update(['path'=>$request->path]);
return response()->json([], 204);
I forget use query() method for ProductMeta and added $table->unique(['product_id', 'key']); to product meta migration.
**products relation one to many with product Meta
And Many to many with content.
Im building CRUD for my application in Laravel 9. I defined some fields in my database as NOT NULL. Those values will be completed by code at the time a new user is created.
The first field Im dealing with is 'creado' (same as 'created at' in english) field (and I dont want to use built in timestamp)
User model:
public $timestamps = false;
protected $fillable = [
'username',
'password',
'apellidos',
'nombres',
'tipoDocumento',
'nroDocumento',
'cuit',
'cuil',
'fechaNacimiento',
'sexo',
'domicilio',
'domicilioNro',
'domicilioPiso',
'domicilioDepto',
'localidad',
'codigoPostal',
'telefonoFijo',
'telefonoCelular',
'telefonoAlt',
'email',
'creado', //This is the value
];
protected $hidden = [
'password',
'password_repeat', //This value is still being displayed in error msg...
'remember_token',
];
protected $casts = [
'creado' => 'datetime', //Dont think its necessary
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
public function setCreadoAttribute()
{
//This method never get called...
$this->attributes['creado'] = date("Y-m-d H:i:s");
}
RegisterRequest:
public function rules()
{
return [
'username' => 'required|unique:clientes,username',
'password' => 'required|min:8',
'password_repeat' => 'required|same:password',
'apellidos' => 'required',
'nombres' => 'required',
'fechaNacimiento' => 'required',
'sexo' => 'required',
'tipoDocumento' => 'required',
'nroDocumento' => 'required|unique:clientes,nroDocumento',
'cuil' => 'nullable|min:11|max:11',
'cuit' => 'nullable|min:11|max:11',
'domicilio' => 'required',
'domicilioNro' => 'required',
'localidad' => 'required',
'codigoPostal' => 'required',
'telefonoCelular' => 'required',
'email' => 'required|unique:clientes,email',
'creado' => 'nullable|date',
];
}
Controller:
public function register(RegisterRequest $request)
{
$cliente = Clientes::create($request->validated());
return redirect("login")->with("success","Bla Bla Bla");
}
In view, all fields are present, except 'creado'. If I add input type text named creado, it works. Else the field its not included in the query. I dont know why its not working when I marked it as nullable.
$casts array and setCreado can be removed.
Then, you can use an accessor, in your model:
protected function creado(): Attribute
{
return Attribute::make(
get: fn ($value) => date("Y-m-d H:i:s"),
);
}
Mas info ;) Accessors
Importante this:
This method name should correspond to the "camel case" representation of the true underlying model attribute / database column when applicable.
Finally, I was able to fix this using this approach:
In ClientsController
public function register(RegisterRequest $request)
{
$cliente = new Cliente;
$cliente = Cliente::make($request->validated());
$cliente->setCreadoAttribute();
$cliente->save();
return redirect("login")->with("success","Cuenta creada exitosamente");
}
In Client model:
public function setCreadoAttribute()
{
$this->attributes['creado'] = date("Y-m-d H:i:s");
}
Im not really sure if this is the best way or the best practice to accomplish what I was needing, but for now it works.
From the docs
If you need to customize the names of the columns used to store the timestamps, you may define CREATED_AT and UPDATED_AT constants on your model:
<?php
class Flight extends Model
{
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'updated_date';
}
I am getting error for undefined method which is defined inside my User model.
My controller:
$inputs = request()->validate([
'title' => 'required|min:8|max:255',
'post_image' => 'file',
'body' => 'required'
]);
auth()->user()->posts()->create($inputs);
My Post model:
public function user() {
return $this->belongsTo('App\Models\User');
}
My User model:
public function posts() {
return $this->hasMany('App\Models\Post');
}
correct your relationship
public function posts() {
return $this->hasMany(Post::class);
}
First your posts relationship is wrong, it must be hasMany NOT belongsTo
public function posts() {
return $this->hasMany(User::class);
}
Then it should work.
You can also try to create the model in a different way:
$validated = request()->validate([
'title' => 'required|min:8|max:255',
'post_image' => 'file',
'body' => 'required'
]);
// Here you should check if $validated has all required fields
// because some could fail, in that case aren't in the array
Post::create([
'title' => $validated['title'],
'user_id' => auth()->id, // or auth()->user->id
'post_image' => $validated['post_image'],
'body' => $validated['body'],
]);
Laravel Validation Docs
I have a field in the in_out model that I want to insert with the requests in the database?
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:255',
'type' => 'required|max:255',
]);
$add = in_out::create($validatedData);
}
Your question is very difficult to understand.
This is an implementation in case you want to add attributes after validation and before storing on DB.
use App\InOut;
class InOutController
{
...
public function store(\Request $request)
{
$rules = [
'name' => 'required|string|max:255',
'type' => 'required|string|max:255',
];
$attributes = $request->validate($rules);
$attributes['description'] = 'Adding a description after validation and before store on database';
$inOut = InOut::create($attributes);
return redirect('/inout')->with('inOut', $inOut)->with('status', 'InOut created.');
}
...
}
In Laravel I have relation:
class Address extends Model
{
protected $fillable = [
'street', 'city', 'post_code', 'country', 'state',
];
public function companies() {
return $this->hasMany('App\Company');
}
}
class Company extends Model
{
protected $fillable = [
'name', 'nip', 'email', 'phone', 'address_id'
];
public function address() {
return $this->belongsTo('App\Address');
}
}
and in CompaniesController.php I want to do update tables. My code looks like this:
public function update(Request $request, $id)
{
Company::where('id', $id)->update([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'nip' => $request->nip,
]);
}
How to also update the address associated with this company?
If the ID is your primary key, you should use find instead of where, as the former will guarantee that you only retrieve one row.
You can then query the relationship of your Company model, using the following code:
$company = Company::find($id);
$company->update([
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'nip' => $request->nip,
]);
$company->address()->update([
'street' => 'street value',
'city' => 'city value',
'post_code' => 'post_code value',
'country' => 'country value',
'state' => 'state value',
]);
Please refer to the laravel docs