get only one row from first table in table left join in laravel - laravel-5

I have two table
1. Bog_post
2. blog_image
Now I want only one image from blog_image table.
I also save post id in blog_image table.
how can i do this query in laravel ?

Create a model as "BlogImage" and edit it.
class BlogImage extends Model
{
protected $table = 'blog_image';
}
Then create a query with this model.
$blog_image = BlogImage::where('post_id',$post_id)->pluck('image')->first();
You can get just "image" column data in this way, as you want.
Also you can set releationship with this two tables. Here is documentation

Related

Laravel - Eloquent relation - many-to-many - get intermediate table columns

I have a question about Laravel Eloquent Relations.
I have the following table situation:
Table guests:
id
name
...
Table landing_pages:
id
name
...
A guest can have several landing pages. And a landing page will have multiple guests.
So I thought, I can use a many-to-many relationship there and I created a new table
Table guests_landing_pages:
id
guest_id
landing_page_id
...
I would be able to use this the following way:
In guest Model I can do this, to create the many-to-many relationship:
public function landing_pages()
{
return $this->belongsToMany(\App\Models\LandingPage\LandingPage::class, 'guests_landing_pages','landing_page_id','guest_id');
}
... and the other way round in landing_page model.
But actually the table guests_landing_pages contains more data than just the guest_id and landing_page_id relation.
There are several other fields in it:
Table guests_landing_pages:
id
guest_id
landing_page_id
identifier_token
max_persons
answer
...
My question is now, how can I realize this best. If I would use the many-to-many relation as described above, I will not be able to access the fields in the intermediate table, won't I?
Or will the best solution to this to create a GuestLandingPages model with all the fields and create hasMany relations to/in both?
Laravel Eloquent has features for retrieving intermediate columns.
public function landing_pages()
{
return $this->belongsToMany(\App\Models\LandingPage\LandingPage::class, 'guests_landing_pages','landing_page_id','guest_id')
->withPivot('max_persons', 'answers');
}

Soft deleting both rows from two tables in Laravel

I'm new to Laravel and trying to soft-delete both rows from two tables.
Vehicles table,
id
license
brand
is_taken
Taken_bies table,
id
name
phone
vehicle_id
In vehicles table, if is_taken is true, I grab the id of vehicle and fill it to vehicle_id from taken_bies table with other information.
In my TakenBy model, I've implemented relationship as follow:
public function vehicle() {
return $this->belongsTo('App\Vehicle');
}
My Requiremnet:
If I soft-delete the vehicle, I want to delete the related taken_bies information from taken_bies table. How can I achieve that? I'm using Laravel 5.8. Thank you.
you can delete it like below
$vehicle = Vehicle::find(1);
$vehicle->taken_bies()->delete();
$vehicle->delete();

Laravel Auditing - How To Get Specific Column Using Eloquent Model Method

I'm using Laravel Auditing Package to trace changes on my models.
I want to get a specific column of the foreign key in it's primary table before recording the events (create, update, delete) in the audit table.
There is a way the package helps get all the attribute of the foreign key, it's called Audit Transformation but it generates an error for me when displaying the details in the table i want to know if there's any eloquent model method to get the specific column info i need instead of using getattribute() method which gets the entire row of the item_id.
Audit Transformation Method
public function transformAudit(array $data): array
{
if (Arr::has($data, 'new_values.item_id')) {
$data['old_values']['item_name'] = Item::find($this->getOriginal('item_id'));
$data['new_values']['item_name'] = Item::find($this->getAttribute('item_id'));
}
return $data;
}
This is how it's stores in the database ephasis on the item_name.
{"item_id":"1","qty_received":"2","delivered_by":"John",
"received_by":"Abi","supplier":"Stores","date":"2019-11-26","item_name":{"item_id":1,"item_name":"Toner","colour":"Black","status":"Active",
"created_at":"2018-10-25 17:55:26","updated_at":"2018-10-25 17:55:26"}}
And this is the Item table schema
So in my scenario i'd want to store the item_name as Toner not the entire row of the item_id
Any suggestion will be welcomed, thanks in advance.
Add ->item_name->item_name after the function of Find.
if (Arr::has($data, 'new_values.item_id')) {
$data['old_values']['item_name'] = Item::find($this->getOriginal('item_id'))->item_name->item_name;
$data['new_values']['item_name'] = Item::find($this->getAttribute('item_id'))->item_name->item_name;
}

Laravel 5.5 many to many relationship with an intermediate table

Please help,
I have these relationship in laravel between ITEMS, SUPPLIERS and PURCHASE_ORDERS
As ITEMS and SUPPLIERS has many to many relationship, I created an intermediate table between them, called ITEM_SUPPLIER.
Now I need to make a many to many relationship between PURCHASE_ORDERS and the intermediate table ITEM_SUPPLIER.
How do I make the relationship?
Should I create an intermediate table between them, what's the best way to name it?
Add a model for pivot table ItemSupplier whth extra column id which extends Pivot
use Illuminate\Database\Eloquent\Relations\Pivot;
class ItemSupplier extends Pivot
{
public function purchaseOrder()
{
return $this->belongsToMany(PURCHASE_ORDERS::class, item_supplier_purchase_order);
}
}
As you see you need to create a new item_supplier_purchase_order pivot table and add the relationships
class PURCHASE_ORDERS extends Model
{
public function purchaseOrder()
{
return $this->belongsToMany(ItemSupplier::class, item_supplier_purchase_order);
}
}
Add id column to the item_supplier and create ItemSupplier model for the pivot table. Then create a new item_supplier_purchase_order pivot table and two belongsToMany() relationships between ItemSupplier and PurchaseOrder models.
Also, don't forget to use cascade deleting to delete records from new pivot table when a related record is deleted from item_supplier pivot table.

Eloquent relationship with 3 tables

I'm new to Stack Overflow and Laravel.
I try to develop a variable profilesystem in Laravel and I got 3 tables ('User', 'User_Fields', 'Fields').
The structure of the Fields table is following:
The structure of the user_fields table is following:
The User table is the standard table which came with Laravel 5
Now I want to get all fields from user_fields depending on which user is selected or logged in. If the user_field doesn't exists, the model should return null or create a new record for the user_field.
I tried a lot of "solutions" which came with eloquent like (hasManyThrough, belongsToMany) but I don't get the result I wanted.
Is there any solution with relationship methods?
Thanks for your help, and sorry for my bad english :/
You should be using the belongsToMany()->withPivot() to retrieve the intermediate table columns. Your user_fields is your pivot table so on your user model you should have:
class User extends Model
{
public function fields()
{
return $this->belongsToMany(Field::class, 'user_fields')->withPivot('value');
}
}
Then you can access your values for a user by:
$user = User::find($id);
foreach($user->fields as $field) {
$field->pivot->value;
}

Resources