I'm trying to use fill() to update a current row in the database. However, it seems to be creating a new row instead of updating each time.
Does anyone know what could be wrong?
Here's my code:
$reserve->where('cookie_id', 'idCode')->first();
$reserve->fill($request->all())->save();
return Redirect::to('checkout');
Try something similar to this
$user = User::where ('cookie_id', 'idCode');
$new_user_data = $request->all();
$user->fill($new_user_data);
$user->save();
You can also try using update()
$affectedRows = User::where('votes', '>', 100)->update($request->all());
Judging by your code you've misunderstood how to fetch an instance out the database. See http://laravel.com/docs/5.1/eloquent#retrieving-single-models
Try the following
$reserve = Reserve::where('cookie_id', $id)->first();
$reserve->fill($request->input())->save();
return redirect()->to('checkout');
Related
I have a Laravel code that i used to insert data into a table, but i want to update the content of another table based on the details of the item inserted; below is my controller code
public function docadd(Request $request){
$doc=new documents();
$doc->claim_id=request('id');
$doc->file_name=request('new');
$doc->cat=request('new');
$doc->type='needed';
$doc->description='needed';
$doc->save();
$id=$doc->claim_id;
$nm=$doc->file_name;
$dc=options::where('claim_id', $id)
->get();
$ed=$dc[0][$nm];
dd($dc[0]->$ed); //this dd returns "Doc2"
$dc->$ed=''; i want it to update the content of the table column with the column with
name of the dd value
$dc->save();
return redirect()->back();
}
pls i really need help with this
I think you have a logic error itself. If you want to update the data, use first() instead of get(), it will return the initial value. May be like this:
public function docadd(Request $request){
$doc= new documents();
$doc->claim_id=request('id');
$doc->file_name=request('new');
$doc->cat=request('new');
$doc->type='needed';
$doc->description='needed';
$doc->save();
//$id=$doc->claim_id;
$nm=$doc->file_name;
$dc=options::where('claim_id', $doc->claim_id)
->first();
//Also you can use like this
//$dc= options::findOrFail($doc->claim_id);
$dc->$ed=''
$dc->save();
return redirect()->back();
}
Then you can do something if I understand you right.
I have a query to select all users and hash the password and save that model. Because it times out as there is large data in the DB, I thought I will try chunk() function. My query looks like,
$users = User::where('password','!=','0')->select(array('password'))->chunk(50,function($users){
foreach ($users as $user) {
$user->password = Hash::make($user->password);
$user->save();
}
});
This doesn't save a model. when I try to dump the $user variable after save() it displays the updated values but when I look into DB it is still unaltered. I tried using try catch and transaction just to see if it hits any exception during the process, nothing helped. Any help will be appreciated. Also, I don't have $guarded on the password field.
You're just doing it wrong. Can you tell which row should be updated in that loop? No? Eloquent doesn't know it either, so it runs update ... where id is null ;)
Simply add id to the select clause and it will work (unless you have some magic happening in the saving event etc):
$users = User::where('password','!=','0')
->select(array('id', 'password'))
->chunk(50,function($users){
foreach ($users as $user) {
$user->password = Hash::make($user->password);
$user->save();
}
});
I'm trying to do a mass update on an eloquent collection.
So I have my query, which looks a bit like this:
\Responder::with('details')
->where('job_number', $project->job_number)
->where('batch_id', ((int) $batch_id) - 1)
->where('updated_at', '<=', $target_time)
->whereHas('transactions', function($q) {
$q->where('status', 'success');
}, '<', 1)
->whereHas('details', function($q) {
$q->where('email', '<>', '');
});
This query object is stored as $query (because I'm re-using it - the same reason I dont want to switch how I'm doing the query), I am then performing an update on the collection, e.g.
$query->update(array('batch_id' => $batch_id));
This works great except it updates all the 'updated_at' timestamps. Now i like the timestamps, they are used extensively elsewhere, so i cant turn them off all together but I thought I could disable them temporarily but I've tried the following:
$query->timestamps = false;
$query->update(array('email_drop_off_index' => $batch_id));
and I can confirm that doesn't work, is there a way to do this?
Any help much appreciated
timestamps = false should be made on your model, but what you are doing is setting the value on the query builder. That's why it is not being picked up.
timestamps is an instance variable so you can't set it statically, and I don't think there is a built-in way to do it from the query builder. So I suggest try instantiating the model first, then create a new query from it, like this:
$responder = new \Responder;
$responder->timestamps = false;
$query = $responder->newQuery()
->with('details')
->where('job_number', $project->job_number)
...; // the rest of your wheres
$query->update(array('email_drop_off_index' => $batch_id));
Here's a possible solution: subclass your Responder model and turn off timestamps in the subclass.
class MassUpdateResponder extends Responder
{
public $timestamps = false;
}
Then use your new class to do the updates. This seems like a bit of a hack, but it should work.
BTW, doing an update like the following worked for me:
$query->timestamps = false;
$query->value = "new value";
$query->save();
The update() method may be doing something different that's causing it to ignore the value of $timestamps.
I'm using Laravel 4. Say I have an Eloquent model (Patient) and I want to get a patient with the name Bob, I would do this:
$patient = Patient::where('name', '=', 'Bob');
What is the best way to check to see if $patient is a valid record?
If the database query does not find any matching results, it returns null. Therefore...
$patient = Patient::where('name','=','Bob')->first();
if ( is_null($patient) ) {
App::abort(404);
}
(Note: in your original question you forgot ->first() (or ->get()) in your query. Don't forget that or else you will get an Eloquent object instead of a result.)
use this:
$patient = Patient::where('name', '=', 'Bob')->firstOrFail();
it will return Eulqouent model on success or throw ModelNotFoundException upon failure.
I know this is old, but this came up as the 2nd google hit on a search, so . . . for cases where you are not expecting one record or cannot use ->firstOrFail() (my use case is an async typeahead api that returns up to 10 results) the only thing that worked for me was count():
$patient = Patient::where('name', '=', 'Bob')->get(); //you could have more than one bob
if (!count($patient)) {
return 'No records found';
}
$patient = Patient::where('name','Bob')->get();
if ( $patient->isEmpty() ) {
return response(['error' => 'Record not found'], 404);
}
Something like Patient::where('name', '=', 'Bob')->exists() may work. It will return a boolean.
Just use empty() from native php will solve everything, if object null it will return true, if laravel's collection from query builder is empty (but initialized) it will return true too.
$contributor = Contributor::whereVendor('web')->first();
if(empty($contributor)){
...
}
use findOrFail($id) in case of you are passing id parameter to fetch a single record
I ended up on this while seeking solution of ::find($id)->firstOrFail syntax which is error-full.
$patient = Patient::findOrFail($id);
In Kohana V3 is it possible to return result set as an array() or any method exists?
For example:
$user = DB::select('*')->from("users")->where('username', '=', $username);
If method is there,then it is possible to get password like
echo $user->password;
Is it possible without ORM? Please suggest.
I think the following would give you all results:
$user = DB::select('*')->from("users")->where('username', '=', $username)->as_object()->execute();
Whereas the following here, would give you the first item:
$user = DB::select('*')->from("users")->where('username', '=', $username)->as_object()->execute()->current();
Try: KO3 Database Wiki
You just need to add a ->current() to the end of your query:
$user = DB::select('*')->from("users")->where('username', '=', $username)->execute()->current();