Query builder with multiselect data - laravel

I've a multiselect in a form which return an array (method=get).
In the DB I've the relative field (varchar) with stored something like:
["apple","lemon","banana"]
How to search in the DB with query builder to check if array values returned from the form are in the DB?
$items = Item::where(.....

You can do something like this
$items = Item::whereIn('name', $array)->get();

Related

How can I get the data inside my notifications from within my Controller?

I'm trying to get the numbers of records from my notifications, where the candidate_user_id column from inside the data attribute is the same as the UserId (Authenticated User).
After I dd, I was able to get the data from all of the records in the table by using the pluck method (Line 1). I then tried to use the Where clause to get the items that I need
but this just didn't work, it was still returning all of the records in the table.
DashboardController.php:
public function index()
{
$notifications = Notification::all()->pluck('data');
$notifications->where('candidate_user_id', Auth::user()->id);
dd($notifications);
}
Here is a partial screenshot of the data that is being plucked.
How can I get the data from this, in a way like this ->where('candidate_user_id', Auth::user()->id);?
If data was a JSON field on the table you could try to use a where condition to search the JSON using the -> operator:
Notification::where('data->candidate_user_id', Auth::id())->pluck('data');
Assuming you only want this data field and not the rest of the fields, you can call pluck on the builder directly. There isn't much reason to hydrate Model instances with all the fields to then just pluck a single field from them if it is just a table field, so you can ask the database for just the field you want.
The data in the data field is a json string, so you can tell Laravel to automatically cast it as an array using the $casts property on each of the models that is notifiable.
For instance, if you have a User model which uses the trait (ie has use Notifiable), add this:
protected $casts = [
'data' => 'array',
];
If you want to access all notifications for the auth user.
$user = auth()->user();
dd($user->notifications->pluck('data'));
If you really want to do in your question way, here is how.
$notifications = Notification::all()->pluck('data');
$notifications = $notifications->where('candidate_user_id', Auth::user()->id)
->all();
This assumes you that you did not modify the default laravel notifications relationship and database migration setup. If you have modified some of the default ones, you need to provide how you modify it.

Allow laravel to filter requests based on field types

So i have 300 different fields in my form generating from a database.These fields can be either a drop-down or text field which need to be saved in two different databases.
To get all the fields that are dropdown i will have to use
$request->input('name')
$request->input('email')
$request->input('username')
and 100 other fields.
rather than doing that and to filter requests based on input types
if(input_type==text){
save to this table
}
else if (input type==select){
save to this table
}
User laravel DB class for that.
Consider below example .
$data = array();
if(input_type==text){
$data['field1'] = 'somevalue';
}
else if (input type==select){
$data['field2'] = 'somevalue';
}
$Insert = DB::table('TABLE_NAME')->insert($data);
Make sure that table have detault value for fields which you are not inserting.
You can use something like this. I am guessing you have two different Model class for store in the database
$firstTableFields = [ 'field1', 'field2', 'field3'];
$secondTableFields = [ 'field4', 'field5', 'field6'];
$firstModel = new Model($request->only($firstTableFields));
$firstModel->save();
$secondModel = new Model($request->only($secondTableFields));
$secondModel->save();

is there a way to query and map datas with their correspondance in laravel

How to map data from one table to may other data from other table;
example we have three table region, market, categories, products using eloquent in laravel 5.1
i have tried this but only gives me the last
public function look(Request $request)
{
``$sector_id=$request->input('category');
$cells=Cell::where('sector_id',"=",$sector_id)->get();
foreach ($cells as $cell){
$a=$cell->id;
$markets=Market::where('cell_id',"=",$a)->get();
}
foreach ($markets as $market){
$b=$market->id;
$prices=Price::where('market_id',"=",$b)->get();
}
return view('reports.sector')->with('cells',$cells)->with('markets',$markets)-`>with('prices',$prices);
}
i need to display only the names in above table but what i need is that one elements in each table maps with their corresponding elements from other table how can i do that query.i need all of those data from database please help me i am stuck here.
If you mean three separate tables:
$cells = Cell::where('sector_id', $sector_id)->get();
$markets = Market::whereIn('cell_id', $cells->pluck('id'))->get();
$prices = Price::whereIn('market_id', $markets->pluck('id'))->get();
If you want to display the data in a tree-like structure, and if you have defined your model relations properly you can use eager loading
$cells = Cell::where('sector_id', $sector_id)->with('markets.prices')->get()
Pass cells to the view and you can use the following in your blade template to display the data
#foreach ($cells as $cell)
#foreach ($cell->markets as $market)
#foreach ($market->prices as $price)

About the laravel eloquent model

The laravel API document: Illuminate\Database\Eloquent\Builder::first() description that:
Model|Builder|null first(array $columns = array('*'))
Execute the query and get the first result.
Parameters
array $columns
Return Value
Model|Builder|null
I can't understand the mean of the return value, In which case it will return Model and In which case it will return Builder?
When you have used eloquent model for retrieving first record it return response in Model, and not sure about builder but when you retrieve records using builder it return builder object.
for example, consider we have states table and we are going to retrieve first record using two different method
1) Query Builder
$state_builder = DB::table("states")->first();
2) Eloquent Model
$state_eloquent = State::first();
you can check the difference between both response, and when no record found it will return null.

Magento: Get collection from custom query

We can write a custom query in Magento:
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("insert into tablename values ('aaa','bbb','ccc')");
Now how can I get the output of query into a collection as I get from Mage::getModel()->getCollection ?
Create an object that extends one of the base collection objects, and add your query to the load method.

Resources