Look at my codes
public function store(ProductRequest $request)
{
$product = new Product();
$product->user_id = auth()->user()->id;
$product->title = $request->title;
$product->body = $request->body;
$product->price = $request->price;
$product->categories()->attach($request->category);
$image = $request->image;
$filename = $image->getClientOriginalName();
$image->move(public_path('images/products'), $filename);
$product->image = $image;
return redirect()->route('products.index');
}
I get this error
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into category_product (category_id, product_id) values (1, ?), (2, ?))
But I select a category of product already.
Before
$product->categories()->attach($request->category);
Write,
$product->save();
This will insert the record in the database, and will return the last insert id. Then this will be used to be inserted into your pivot table.
Related
How can I display the last entry in my blade.
I have this request:
#foreach(($listContrat = \App\Stagiaire::join('carrieres', 'stagiaires.id', '=', 'carrieres.stagiaire_id')
->where('stagiaires.id', $id)
->OrderBy('id','desc')
->take(1)
->get())
as $key => $car)
I have this error message with this query
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous (SQL: select * from stagiaires
Thanks freedomn-m
The solution is:
#foreach(($listContrat = \App\Stagiaire::join('carrieres', 'stagiaires.id',
'=', 'carrieres.stagiaire_id')
->where('stagiaires.id', $id)-
>OrderBy('carrieres.id','Desc')->take(1)->get()) as $key => $car)
I have two tables: leads and hts_patients. Both table has phone column and I want to list all leads that has no record on hts_patients table(foreign key is phone).
So far, I have tried this and it works but taking so much long time.
$countries = ['DE','TR'];
$dateS = Carbon::now()->startOfMonth()->subMonth(4);
$dateE = Carbon::now()->startOfMonth();
$leads = Lead::whereBetween('created_at',[$dateS,$dateE])->whereIn('visitor_country',$countries)
->select('id','name','phone','visitor_country','source','description','contact','created_at')->get();
$patient = HtsPatient::pluck('phone');
foreach ($leads as $key=>$item){
if (isset($item->phone)){
$data =$patient->where('phone',$item->phone)->first();
if (!empty($data)){
$leads->forget($key);
}
}
}
return view('poll.leadSecond',['leads' => $leads]);
Thing, that I want is probably something like this;
$countries = ['DE','TR'];
$dateS = Carbon::now()->startOfMonth()->subMonth(4);
$dateE = Carbon::now()->startOfMonth();
$leads = DB::table('leads')->join('hts_patients','hts_patients.phone', '=', 'leads.phone')
->whereBetween('leads.created_at',[$dateS,$dateE])->whereIn('leads.visitor_country',$countries)
->whereNotIn('phone', function($query) {
$query->select('phone')
->from('hts_patients');
})
->select('leads.id','leads.name','leads.phone','leads.visitor_country','leads.source','leads.description','leads.contact','leads.created_at')->get();
But it gives an error says
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'phone'
in IN/ALL/ANY subquery is ambiguous
Any help?
you have tow tables with column called 'phone', in your whereNotIn clause you should specify witch on you wants.
I think the result should be like:
->whereNotIn('leads.phone', function($query) {
$query->select('hts_patients.phone');
// ->from('hts_patients'); you do not need this 'from'
})
I want to join right (employees) table only when left (purchases) table's employee_id is NOT NULL but the query returning empty result set if the employee_id is NULL.
$data = DB::table('purchases')
->join('demands', 'purchases.demand_id', 'demands.id')
->join('fiscal_years', 'purchases.fiscal_year_id', 'fiscal_years.id')
->join('employees', function ($join){
$join->on('purchases.employee_id', 'employees.id')
->where('purchases.employee_id', '!=', null);
})
->select('purchases.*', 'demands.code', 'fiscal_years.year', 'employees.name')
->get();
Try to use leftJoin. It will join employees if they are.
$data = DB::table('purchases')
->join('demands', 'purchases.demand_id', 'demands.id')
->join('fiscal_years', 'purchases.fiscal_year_id', 'fiscal_years.id')
->leftJoin('employees', function ($join){
$join->on('purchases.employee_id', 'employees.id')
->where('purchases.employee_id', '!=', null);
})
->select('purchases.*', 'demands.code', 'fiscal_years.year', 'employees.name')
->get();
Here is my query:
PHP
public function find(array $filter = []) : Collection
{
return $this->entity::where($filter)->join('cases_has_services', function ($join) {
$join->on('cases_has_services.cases_id', 'checklists.cases_id');
$join->on('cases_has_services.services_id', 'checklists.item_id');
})->select('checklists.*', 'cases_has_services.quantity')->get();
}
i am receiving the next error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where
clause is ambiguous (SQL: select `checklists`.*,
`cases_has_services`.`quantity` from `checklists` inner join
`cases_has_services` on `cases_has_services`.`cases_id` =
`checklists`.`cases_id` and `cases_has_services`.`services_id` =
`checklists`.`item_id` where (`id` = 352))
So i fixed my code a little bit:
public function find(array $filter = []) : Collection
{
// TODO fix this
foreach ($filter as $field => $value) {
unset($filter[$field]);
$field = 'checklists.' . $field;
$filter[$field] = $value;
}
return $this->entity::where($filter)->join('cases_has_services', function ($join) {
$join->on('cases_has_services.cases_id', 'checklists.cases_id');
$join->on('cases_has_services.services_id', 'checklists.item_id');
})->select('checklists.*', 'cases_has_services.quantity')->get();
}
But that is not a good practice i think. So, are there any native laravel path to auto insert table name in query with joins?
I mean, how to avoid using that part of code because its a little hacky:
foreach ($filter as $field => $value) {
unset($filter[$field]);
$field = 'checklists.' . $field;
$filter[$field] = $value;
}
Change you $filter variable to:
$filter = ['checklists.id' => 123]
Hi I'm new to Laravel and got stuck in a Problem.
I have a model Contact
class Contact extends Eloquent {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'contacts';
}
The table have these fields:
user_id (int)
contact_id (int)
...
these two fields represent the primary key.
In the ContactsController I have the function store in wich I create or update the database:
public function store()
{
switch (Input::get('type')){
case 'make_contact_request':
$user = User::where('email', '=', Input::get('login'));
$request_self = new Contact;
$request_contact = new Contact;
$request_self->user_id = Auth::user()->id;
$request_self->contact_id = $user->id;
$request_self->status = 2;
$request_self->message = Input::get('message');
$request_contact->user_id = $user->id;
$request_contact->contact_id = Auth::user()->id;
$request_contact->status = 1;
$request_contact->message = Input::get('message');
$request_self->save();
$request_contact->save();
break;
case 'answer_contact_request':
$request_self = Contact::where('user_id', '=',Input::get('contact_id'))->where('contact_id', '=', Auth::user()->id)->first();
//$request_self = Contact::whereRaw('user_id = '.Input::get('contact_id').' AND contact_id = '.Auth::user()->id.' ');
$request_contact = Contact::whereRaw('user_id = '.Auth::user()->id.' AND contact_id = '.Input::get('contact_id').' ');
$request_self->status = 3;
$request_contact->status = 3;
$request_self->save();
$request_contact->save();
break;
}
}
I tried two different ways to get the Contact Object for the request_self Object and I get the following error:
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `contacts` set `status` = 3, `updated_at` = 2014-08-02 16:16:56 where `id` is null)"
for the request_contact Object it throws a fatal error (don't get the description) and close the session.
At the end I am at the beginning of laravel so I hope the solution is pretty easy to find :) but I dont even really know for what to search.
Update:
At the end I fixed the Problem with the update function.
case 'answer_contact_request':
$request_self = Contact::where('user_id', '=',Input::get('contact_id'))->where('contact_id', '=', Auth::user()->id)->update(array('status' => 3));
$request_contact = Contact::where('user_id', '=', Auth::user()->id)->where('contact_id', '=', Input::get('contact_id'))->update(array('status' => 3));
break;
I think you can add
public function scopeComposite($query, $user_id, $contact_id)
{
return $query->where('user_id', '=', $user_id)->where('contact_id', '=', $contact_id);
}
and then you can get the contact with:
$request_self = Contact::composite(Input::get('contact_id'), Auth::user()->id)->get();
source: http://laravel.com/docs/eloquent#query-scopes
I'm not sure you can make it like this.
There is a way to make sure it works:
add a column id ( auto increment, primary ) and make the group ( contact_id, user_id ) unique and you can use query scopes and id based