Larevel Relationship from array in database

I was wondering if you could help with a laravel relationship. I will try to explain as best i can.

I have a two tables:

Alerts

Schema::create('alerts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->float('price_low')->nullable();
$table->float('price_high')->nullable();
$table->json('conditions')->nullable();
$table->softDeletes();
$table->timestamps();
});

Conditions

DB::table('conditions')->insert([
['condition_value' => 'New'],
['condition_value' => 'Second Hand'],
['condition_value' => 'Ex Demo'],
]);

The condition field 'conditions' stores an array like this:

[{"condition_id": 1}, {"condition_id": 2}]

I am trying to define a relationship so i can do something like this:

$alerts = Alerts::with(['condition'])->where('user_id', $request->user()->id)->get();

My Alert model looks like this:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Alerts extends Model
{
protected $casts = [
'conditions' => 'array'
];


public function user(){
return $this->hasOne('App\User', 'id', 'user_id');
}

public function condition(){
return $this->hasMany('App\Condition', 'id', 'conditions[condition_id]');
}
}

but nothing is returned and im struggling to find an answer in the Laravel docs.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2vJFm0x
via IFTTT

تعليقات

المشاركات الشائعة من هذه المدونة

I am unable to figure out how to create payment collection request, after a form has been submitted in laravel

laravel, mysql transaction not working after failed one time