Inside my store function i have to search a name of a particular person, thus:
$person = DB::select("select p.* from persons p where concat_ws(', ' ,p.family_name,concat_ws(' ',concat_ws(' ',p.first_name,p.middle_name),p.third_name)) like ?", [$request['person_name']]);
If this person exist i have to update the person and this one works:
Person::where('id', $person[0]->id)->update(['status' => 'Active']);
but not this one:
$person[0]->status = 'Active';
$pArray = json_decode(json_encode($person[0]), true);
$per = new Person($pArray);
$per->update();
Since you have already created a new model instance, you would need to call the save() method.
$per = new Person($pArray);
$per->save();
Or, you can use update() to pass data into an existing model. But first, you need to retrieve the model you want to update.
$per = Person::find($pArray['id']);
$per->update($pArray);
Use your search query instead of mine. I think this will help you.
$result = User::where('id', $userId)->first();
$result->name = 'xyz';
$result->update();
For update data in laravel using model you need to pass where condition in it.
Sample example
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
Hope this helps you!
Related
function subscriptionpack(Request $request)
{
$sub = subscription::all();
$details = $sub['details'];
dump('$details');
}
I can get all values from this code, but I can't get the specific field that named details. When I call dump($sub) it was shown all details, but the dump('$details') make an error that is undefined index:details
anyone can help me to correct this code?
You can use select method to select specific column data :
$sub = subscription::select('details')->get();
Or, you can do with on get() method as like :
$sub = subscription::get(['details']);
If you need single column from the result set use pluck
$subs = subscription::all();
$details = $subs->pluck('details')->toArray();
You can either loop over to your collection e.g
foreach($sub as $s){
dump($s->details)
}
or you can try something like.
$sub->first()->details
but this will give you only first details element from your collection.
First a model should be in singular, no spacing between words, and capitalised.
You should use Subscription::all(); not subscription::all();. You should read Laravel conventions
use App\Subscription;
..
function subscriptionpack(Request $request)
{
$sub = Subscription::all()->pluck('details');
dd($sub);
}
I have a query where in I use Eloquent for finding the ID but what I need is to directly subtract inside the eloquent query ? same as in Query Builder,
Documentation code
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
What I need is to directly to substract
$flight = App\Flight::find(1);
$flight->value =- 1;
$flight->save();
use laravel increment() or decrement() method see
App\Flight::find(1)->decrement('value',1);
second way u can achieve by update query
App\Flight::where('id', 1)->update(['value' => \DB::raw('value - 1')]);
you need to do this
$flight = App\Flight::find(1);
$flight->value = $flight->value-1;
$flight->save();
hope it helps! :)
I'm new to laravel, so I'm searching for how to send a specific id to the controller in order to get data from another table ?
For example
while($shipment = sqlsrv_fetch_array($get_customer_rule,SQLSRV_FETCH_ASSOC)) {
//display some data ......
// inside the loop i will have another query to get data from another table has //relation with shipment table
$id = $shipment['id'];
$customer = "SELECT * FROM [ccctadm].[customer] WHERE id = '$id' ";
$get_customer_info = sqlsrv_query($conn, $customer);
$get_customer_id = sqlsrv_fetch_array($get_customer_info,SQLSRV_FETCH_ASSOC);
$customer_id = $get_customer_id['customerid'];
}
I can't write query in while loop in laravel, so how can I pass shipment id to the controller so I can get customer data related to the shipment
Since you are new to Laravel, maybe you should learn the Laravel way first. Watch this video on how to work with Eloquent and perhaps every other video in that series. https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
Once you get your head around that, you will be able to rewrite your query as
$shipments = Shipement::all();
foreach( $shipments as $shipment ) {
$customer = $shipment->customer;
$customer_id = $customer->id;
}
Even better when you get a bit further with laravel and be able to work with eager loading, you will just do
$shipments = Shipment::with('customer')->get();
And in your view
#foreach($shipments as $shipment)
Customer ID is : {{ $shipment->customer->id }}
#endforeach
You have decided you to work with Laravel. Take advantage of it. It will make everything easier and speed up your development process.
If you want to stick to your raw SQL queries, you can use the query builder
$result = DB::select("SELECT * FROM [ccctadm].[customer] WHERE id = ?", [$id]);
And work with the result
I have two models which are joined by a pivot table, User and Task.
I have a user_id and a task_id.
What is the neatest way to check whether a record exists for this combination of user and task?
You have a couple options depending on your situation.
If you already have a User instance and you want to see if it has a task with a certain id, you can do:
$user = User::find(1);
$hasTask = $user->tasks()->where('id', $taskId)->exists();
You can reverse this if you have the Task instance and want to check for a user:
$task = Task::find(1);
$hasUser = $task->users()->where('id', $userId)->exists();
If you just have the ids, without an instance of each, you could do the following:
$hasPivot = User::where('id', $userId)->whereHas('tasks', function ($q) use ($taskId) {
$q->where('id', $taskId);
})
->exists();
You can also try this, this is working in Laravel 5.7
$user = User::find(1);
$hasTask = $user->tasks->contains($taskId);
But the better way will be
$hasTask = $user->tasks()->where('tasks.id', $taskId)->exists();
may be you search this?
$users = User::has('task')->get();
Easiest to read for me is:
$user->tasks()->exists();
You can use the code below:
$user = User::find(1);
$hasTask = $user->tasks()->where('task_id', $taskId)->exists();
I want to find all patients that belong to a user where id = 1
This works:
$data = Patient::where('user_id', '=', 1)
->with('method', 'images')->get()->toJson();
This doesn't work:
$data = User::find(1)->patients->with('method', 'images')->get()->toJson();
It says:
Call to undefined method Illuminate\Database\Eloquent\Collection::with()
Why is it wrong? Could it be corrected?
The reason your code doesn't work is all Eloquent relationship declaration returns different result depending on whether you are trying to access the relationship as property or as method (with () or without ()).
// Return you chainable queries
$query = User::find(1)->patients()->...
// Return you collection of patients
$patientsCollection = User::find(1)->patients;
Try
User::find(1)->patients()->with('method', 'images')->get()->toJson();
Try this
$patient = New Patient;
$data = $patient->where('user_id','=',1)->with('method','images')->get()->toJson();