I have a problem. I had this code:
$model=VehicleModel::where('make_id','=',$make_id)->get();
return Response::json($model);
Its worked good,now I need to change something like that:
$maketable=$vehicletype.'_models';
$models=new VehicleModel;
$models->setTable($maketable);
$models->where('make_id',$make_id);
$model=$models->get();
return Response::json($model);
So,I added only just to set other table,and now the ->where is not working. Any idea?
Just use Query Builder:
$data = DB::table($maketable)->where('make_id', $make_id)->get();
Try this:
$data = DB::table($maketable)->whereMakeId($make_id)->get();
Related
I have a query like this:
$blog = BlogModel::select('user_id')->get();
and it return this
[{"user_id":2},{"user_id":3},{"user_id":4},{"user_id":4},{"user_id":6}]
I would like Delete duplicate user_id like this
[{"user_id":2},{"user_id":3},{"user_id":4},{"user_id":6}]
You can use DISTINCT for that purpose
$blog = BlogModel::select('user_id')->distinct()->get();
You can use distinct() to force the query to return distinct results.
Try change this:
$blog = BlogModel::select('user_id')->get();
To:
$blog = BlogModel::select('user_id')->distinct()->get();
You can read more here:
https://laravel.com/docs/9.x/queries
$blog = BlogModel::selectRaw('distinct user_id')->get();
Item Model:
public function item_makes(){
return $this->hasMany(ItemMake::class,'item_id','id');
}
In ItemMake Model :
public function make(){
return $this->belongsTo(Make::class,'make_id','id');
}
I need to get array of all make based on item_id. How to achieve this? Thanks in Advance.
This worked for me.
$item = Item::findOrFail(1)
->item_makes()
->with('make')
->get()
->pluck('make')
->flatten()
->toArray();
Try something like this:
Item::findOrFail(1)
->with('item_makes.make')
->get()
->pluck('make')
->flatten()
->toArray()
Try wherehas method something like this
$makes = Make::whereHas('item_makes', function ($query) use($item_id) {
$query->where('item_id', $item_id);
})->get();
I have a piece of code like this:
$products = Product::all()
if ($search_value) {
$products = $products->where('name', 'LIKE', "%$search_value%");
}
$products = $products->orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
I got the following error:
BadMethodCallException in Macroable.php line 81:
Method orderBy does not exist.
I guess orderBy need to follow Product:: directly, but I can't save $products = Product::, can I?
Any suggestions? Thanks.
You're trying to use orderBy() method on Eloquent collection. Try to use sortByDesc() instead.
Alternatively, you could change $products = Product::all(); to $products = new Product();. Then all your code will work as you expect.
just use the one line code it will work fine
$product= Product::orderBy('created_at','desc')->get();
use sortByDesc('id') or simple sortBy() inside use the variable through which you wanna sort like i add id
If you want to get the list of all data and grab it in descending order try this:
$post = Post::orderBy('id', 'DESC')->get();
You are first getting all() data and then trying to sort which is wrong. You have to fix this by removing
$products = Product::all()
and changing your code into something like this
if ($search_value) {
$products = Product::where('name', 'LIKE', "%$search_value%");
}
else {
$products = Product::orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
}
Hope you get idea to tweak your code.
Your query is wrong.
remove all from $products = Product::all() and then put get() at the end of your query.
$table_Data = DB::table('tbl_product')->orderBy('id','DESC');
You can use this...
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');
I am trying to do this sql query on codeigniter
SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'
I am not sure if it is correct...
$this->db->get_where('person' , array('type'=>'staff'))
->like('description','university%')
->result_array();
does anyone have an idea about my case? thanks in advance ...
Using Active Record you can do so
$query = $this->db->select('*')
->from('person')
->where('type','staff')
->like('description','university','after')
->get();
$result = $query->result_array();
Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'
The docs has this explained very well, but their API is very simple. To produce the query you need the relevant code which looks like this:
$this->db->select('*')
->from('person');
->where('type', 'staff')
->like('description','university');
$query = $this->db->get();
$result = $query->result_array();
I never used chaining(even though i know it's possible), but breaking down your question should be easy ;
$this->db->from('person');
$this->db->where('type','staff');
$this->db->where('description', 'university%');
$query = $this->db->get();
$result = $query->result_array();
return $result;