Method Illuminate\Support\Collection::save does not exist - php

I want to create update function, but the error keep shown. Is it because of the join query or the get() or first() method. I dont know how to solve this.
Method Illuminate\Support\Collection::save does not exist.
Controller:
public function update(Request $request, $id)
{
// dd($request);
$request->validate([
'first_name' => 'max:150',
'last_name' => 'max:150',
'userD_ic' => 'max:12',
'userD_position' => 'max:50',
'userD_epf' => 'max:20',
'userD_socso' => 'max:20',
'userD_passport' => 'max:30',
'user_id' => 'numeric',
'ea_serial_no' => 'required|numeric',
'ea_employer_id' => 'required|numeric',
'ea_employee_income_tax_no' => 'required|numeric',
'ea_lhdn_branch' => 'required|max:255',
]);
$eaforms = DB::table('ea')
->join('users', 'ea.user_id', '=', 'users.id')
->join('user_details', 'ea.user_id', '=', 'user_details.user_id')
->where('ea.id', $id)
->select('ea.*', 'users.first_name', 'users.last_name', 'user_details.userD_position', 'user_details.userD_ic', 'user_details.userD_passport', 'user_details.userD_epf', 'user_details.userD_socso')
->get();
$eaforms->first_name = $request->first_name;
$eaforms->last_name = $request->last_name;
$eaforms->userD_ic = $request->userD_ic;
$eaforms->userD_position = $request->userD_position;
$eaforms->userD_epf = $request->userD_epf;
$eaforms->userD_socso = $request->userD_socso;
$eaforms->userD_passport = $request->userD_passport;
$eaforms->user_id = $request->user_id;
$eaforms->ea_serial_no = $request->ea_serial_no;
$eaforms->ea_employer_id = $request->ea_employer_id;
$eaforms->ea_employee_income_tax_no = $request->ea_employee_income_tax_no;
$eaforms->ea_lhdn_branch = $request->ea_lhdn_branch;
$eaforms->save();
return redirect()->route('ea.view', $eaforms->id)->with('update', 'add');
}
Model:
class EaForms extends Model
{
protected $table = 'ea';
use HasFactory, SoftDeletes;
protected $fillable = [
'user_id',
'ea_year',
'ea_serial_no',
'ea_employer_id',
'ea_employee_income_tax_no',
'ea_lhdn_branch',
'created_at',
'updated_at',
'deleted_at',
];
public function users()
{
return $this->belongsTo(User::class);
}
}

Can you try this (With your relationship method called users)
$eaforms = EaForms::where('id',$id)->first();
// Update user data
$eaforms->first_name = $request->first_name;
$eaforms->last_name = $request->last_name;
$eaforms->userD_ic = $request->userD_ic;
$eaforms->userD_position = $request->userD_position;
$eaforms->userD_epf = $request->userD_epf;
$eaforms->userD_socso = $request->userD_socso;
$eaforms->userD_passport = $request->userD_passport;
// Update EA Forms
$eaforms->users->user_id = $request->user_id;
$eaforms->users->ea_serial_no = $request->ea_serial_no;
$eaforms->users->ea_employer_id = $request->ea_employer_id;
$eaforms->users->ea_employee_income_tax_no = $request->ea_employee_income_tax_no;
$eaforms->users->ea_lhdn_branch = $request->ea_lhdn_branch;
$eaforms->users->save();
$eaforms->save();

Related

generating dynamic pdf with DOMPDF in laravel

I am trying to generate an invoice pdf with dynamic data (mostly taken from a form). This is the code:
class PDFController extends Controller
{
public function invoice() {
$institution = $this->institution();
$user = $this->user();
$invoice = $this->invoice_form();
return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user]);
}
public function institution() {
$institution = Institution::where('id', 1)->get()->first();
return $institution;
}
public function user() {
$user = Auth::user();
return $user;
}
public function invoice_form(Request $request) {
$this->validate($request, array(
'furnizor-select' => 'required',
'document-number' => 'required',
'document-date' => 'required',
'due-date' => 'required',
'discount-procent' => 'required',
'discount-value' => 'required',
'total-value' => 'required',
'nir-number' => 'nullable'
));
$invoice = new \App\Models\Invoice();
$invoice->provider_id = $request->input('furnizor-select');
$invoice->number = $request->input('document-number');
$invoice->document_date = $request->input('document-date');
$invoice->due_date = $request->input('due-date');
$invoice->discount_procent = $request->input('discount-procent');
$invoice->discount_value = $request->input('discount-value');
$invoice->total = $request->input('total-value');
$invoice->save();
$invoices = Invoice::all();
$invoice_id = $invoices->last()->id;
$old_date = $request->input('document-date');
$new_date = date("d-m-Y", strtotime($old_date));
$provider_id = $request->input('furnizor-select');
$provider = Provider::where('id', $provider_id)->get();
$invoice_number = $request->input('document-number');
$old_due_date = $request->input('due-date');
$new_due_date = date("d-m-Y", strtotime($old_due_date));
$filename = 'pdfs/nir'.$invoice_id.'.pdf';
}
}
However, I am getting this error:
Too few arguments to function App\Http\Controllers\PDFController::invoice_form(), 0 passed in /Users/cristimamota/NEWSAJ BACKUP PRE FINAL/app/Http/Controllers/PDFController.php on line 17 and exactly 1 expected
And this is because in the invoice() function, I am calling the invoice_form() function and is getting no parameter.. I think this is not the correct way to do it. How should I approach it?
Since your invoice_form(Request $request) requires a Request model as parameter you can change your invoice() method to take $request as parameter and pass that $request to your invoice_form() method. Change
public function invoice() {
$institution = $this->institution();
$user = $this->user();
$invoice = $this->invoice_form();
return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user]);
}
to
public function invoice(Request $request) {
$institution = $this->institution();
$user = $this->user();
$invoice = $this->invoice_form($request);
return view('pdf-generation.invoice')->with([
'institution' => $institution,
'user' => $user
]);
}

How to ignore unique validation when updating fields in laravel8?

I currently use Laravel 8 and I want to know how do I ignore unique validation when a user is updating their profile? If the user is updating every field except the pageName I don't want it to throw a vaildation error cause the user is already the owner of that pageName. I tried this code but it gives error: ErrorException
Undefined variable: user
$request->validate([
'image' => 'nullable|mimes:jpeg,jpg,png|max:100',
'pageName' => 'nullable|alpha_dash|unique:users,littlelink_name'.$user->id,
'pageColor' => 'nullable',
'pageFontcolor' => 'nullable',
'pageDescription' => 'nullable|regex:/^[\w.\- ]+$/i',
'pagePixiv' => 'nullable|url',
]);
This is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Auth;
use DB;
use App\Models\User;
use App\Models\Button;
use App\Models\Link;
class UserController extends Controller
{
//Statistics of the number of clicks and links
public function index()
{
$userId = Auth::user()->id;
$littlelink_name = Auth::user()->littlelink_name;
$links = Link::where('user_id', $userId)->select('link')->count();
$clicks = Link::where('user_id', $userId)->sum('click_number');
return view('studio/index', ['littlelink_name' => $littlelink_name, 'links' => $links, 'clicks' => $clicks]);
}
//Show littlelink page. example => http://127.0.0.1:8000/+admin
public function littlelink(request $request)
{
$littlelink_name = $request->littlelink;
$id = User::select('id')->where('littlelink_name', $littlelink_name)->value('id');
if (empty($id)) {
return abort(404);
}
$information = User::select('littlelink_name', 'littlelink_color', 'littlelink_fontcolor', 'littlelink_pixiv', 'littlelink_description')->where('id', $id)->get();
$links = DB::table('links')->join('buttons', 'buttons.id', '=', 'links.button_id')->select('links.link', 'links.id', 'buttons.name')->where('user_id', $id)->orderBy('up_link', 'asc')->get();
return view('littlelink', ['information' => $information, 'links' => $links, 'littlelink_name' => $littlelink_name]);
}
//Show buttons for add link
public function showButtons()
{
$data['buttons'] = Button::select('name')->get();
return view('studio/add-link', $data);
}
//Save add link
public function addLink(request $request)
{
$request->validate([
'link' => 'required|url',
'button' => 'required'
]);
$link = $request->link;
$button = $request->button;
$userId = Auth::user()->id;
$buttonId = Button::select('id')->where('name' , $button)->value('id');
$links = new Link;
$links->link = $link;
$links->user_id = $userId;
$links->button_id = $buttonId;
$links->save();
return back()->with('message', 'Link Added');
}
//Count the number of clicks and redirect to link
public function clickNumber(request $request)
{
$link = $request->link;
$linkId = $request->id;
if(empty($link && $linkId))
{
return abort(404);
}
Link::where('id', $linkId)->increment('click_number', 1);
return redirect()->away($link);
}
//Show link, click number, up link in links page
public function showLinks()
{
$userId = Auth::user()->id;
$data['links'] = Link::select('id', 'link', 'click_number', 'up_link')->where('user_id', $userId)->orderBy('created_at', 'desc')->paginate(10);
return view('studio/links', $data);
}
//Delete link
public function deleteLink(request $request)
{
$linkId = $request->id;
Link::where('id', $linkId)->delete();
return back();
}
//Raise link on the littlelink page
public function upLink(request $request)
{
$linkId = $request->id;
$upLink = $request->up;
if($upLink == 'yes'){
$up = 'no';
}elseif($upLink == 'no'){
$up = 'yes';
}
Link::where('id', $linkId)->update(['up_link' => $up]);
return back();
}
//Show link to edit
public function showLink(request $request)
{
$linkId = $request->id;
$link = Link::where('id', $linkId)->value('link');
$buttons = Button::select('name')->get();
return view('studio/edit-link', ['buttons' => $buttons, 'link' => $link, 'id' => $linkId]);
}
//Save edit link
public function editLink(request $request)
{
$request->validate([
'link' => 'required|url',
'button' => 'required',
]);
$link = $request->link;
$button = $request->button;
$linkId = $request->id;
$buttonId = Button::select('id')->where('name' , $button)->value('id');
Link::where('id', $linkId)->update(['link' => $link, 'button_id' => $buttonId]);
return redirect('/studio/links');
}
//Show littlelinke page for edit
public function showPage(request $request)
{
$userId = Auth::user()->id;
$data['pages'] = User::where('id', $userId)->select('littlelink_name', 'littlelink_color', 'littlelink_fontcolor', 'littlelink_pixiv', 'littlelink_description')->get();
return view('/studio/page', $data);
}
//Save littlelink page (name, description, logo)
public function editPage(request $request)
{
$request->validate([
'image' => 'nullable|mimes:jpeg,jpg,png|max:100',
'pageName' => 'nullable|alpha_dash|unique:users,littlelink_name'.$user->id,
'pageColor' => 'nullable',
'pageFontcolor' => 'nullable',
'pageDescription' => 'nullable|regex:/^[\w.\- ]+$/i',
'pagePixiv' => 'nullable|url',
]);
$userId = Auth::user()->id;
$littlelink_name = Auth::user()->littlelink_name;
$profilePhoto = $request->file('image');
$pageName = $request->pageName;
$pageColor = $request->pageColor;
$pageFontcolor = $request->pageFontcolor;
$pageDescription = $request->pageDescription;
$pagePixiv = $request->pagePixiv;
User::where('id', $userId)->update(['littlelink_name' => $pageName, 'littlelink_color' => $pageColor, 'littlelink_fontcolor' => $pageFontcolor, 'littlelink_pixiv' => $pagePixiv, 'littlelink_description' => $pageDescription]);
if(!empty($profilePhoto)){
$profilePhoto->move(public_path('/img'), $littlelink_name . ".png");
}
return back()->with('message', 'Saved');
}
//Show user (name, email, password)
public function showProfile()
{
$userId = Auth::user()->id;
$data['profile'] = User::where('id', $userId)->select('name', 'email')->get();
return view('/studio/profile', $data);
}
//Save user (name, email, password)
public function editProfile(request $request)
{
$request->validate([
'name' => 'required|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
]);
$userId = Auth::user()->id;
$name = $request->name;
$email = $request->email;
$password = Hash::make($request->password);
User::where('id', $userId)->update(['name' => $name, 'email' => $email, 'password' => $password]);
return back();
}
}
Anyone know how to fix this?
You can change your code to the following
$userId = Auth::user()->id;
$request->validate([
'image' => 'nullable|mimes:jpeg,jpg,png|max:100',
'pageName' => 'nullable|alpha_dash|unique:users,littlelink_name,'.$userId,
'pageColor' => 'nullable',
'pageFontcolor' => 'nullable',
'pageDescription' => 'nullable|regex:/^[\w.\- ]+$/i',
'pagePixiv' => 'nullable|url',
]);
you can use the Rule object to make that validation:
'pageName' => ['nullable','alpha_dash', Rule::unique('users','littlelink_name')->ignore($user->id)

i have a problem with saving data to database using LARAVEL

this is all my codes after cliking on button doesn't save to database i try to change method of saving but not work
use App\makereservations;
use Illuminate\Http\Request;
class reservationController extends Controller
{
public function hreservation()
{
return view('hreservation');
}
public function reserve (Request $request)
{
$this->validate($request ,[
'name' => 'required',
'email' => 'required',
'phone' => 'required|email',
'date' => 'required',
'time' => 'required',
]);
$reservation = new makereservations();
$reservation = $request->input('name');
$reservation = $request->input('email');
$reservation = $request->input('phone');
$reservation = $request->input('date');
$reservation = $request->input('time');
$reservation = $request->input('personne');
$reservation -> statu = false;
$reservation -> save();
return redirect()->back();
}
}
this is the roote
route::post('reserve', 'reservationController#reserve' )->name('reserv.sent');
You missed the -> arrow sign.
$reservation = new makereservations();
$reservation->name = $request->input('name');
$reservation->email = $request->input('email');
$reservation->phone = $request->input('phone');
$reservation->date = $request->input('date');
$reservation->time = $request->input('time');
$reservation->personne = $request->input('personne');
$reservation-> statu = false;
$reservation-> save();
return redirect()->back();
If in the MakeReservations model you have massively assignable fields
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class makereservations extends Model
{
protected $table = "your_table_name"
...
protected $fillable = [
'name', 'email', 'phone', 'date', 'time', 'personne', 'status'
];
...
}
Your reserve method in reservationController it may be so:
<?php
namespace App\Http\Controllers;
use App\makereservations;
use Illuminate\Http\Request;
class reservationController extends Controller
{
...
public function reserve (Request $request)
{
$this->validate($request ,[
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'date' => 'required',
'time' => 'required',
]);
$inputs = $request->all();
$inputs['status'] = false;
$reservation = makereservations::create($inputs);
return redirect()->back();
}
}
I have detected that in your validation method you are trying to validate that the phone is an email
You can do it more shortly and in a clean way, to optimize your code.
public function reserve (Request $request)
{
$attributes = $this->validate($request ,[
'name' => 'required',
'email' => 'required|email',
'phone' => 'required',
'date' => 'required',
'time' => 'required',
'personne' => ''
]);
$attributes['statu'] = false;
makereservations::create($attributes);
return redirect()->back();
}
In your model make sure these things
class makereservations extends Model
{
protected $table = 'reservation';
protected $guarded = []; //it means all data from request will be mass assignable
}

Creating default object from empty value in Laravel 6.2

I want to Update this data in database.
error :
ErrorException
Creating default object from empty value
i get error in this line :
$user->name = $request->name;
My Controller code is :
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|max:255',
'mobile' => 'required|numeric|regex:/(0)[0-9]/|not_regex:/[a-z]/|digits:11',
'national_code' => 'required|numeric|regex:/(0)[0-9]/|not_regex:/[a-z]/|digits:10',
'avatar' => 'required|mimes:jpg,jpeg,png',
]);
$id = (int)$id;
$user = User::findOrFail($id);
$user->name = $request->name;
$user->mobile = $request->mobile;
$user->national_code = $request->national_code;
$user->province = $request->province;
$user->city = $request->city;
$user->address = $request->address;
$user->postcode = $request->postcode;
$user->active = $request->active;
$user->avatar = $request->avatar;
if(! is_null($request->password)) {
$request->validate([
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$user->password = $request->password;
}
$user->active = $request->active ? 1 : 0;
if ($request->role_id) {
$user->syncRoles($request->role_id);
}
$user->save();
$notification = array(
'message' => 'Success !',
'alert-type' => 'success'
);
return redirect(route('management.users.index'))->with($notification);
}
Route :
Route::resource('users', 'UserController');
Thank you
Try this:
$user = User::find($id)
or this:
$user = User::where('id', $id)->first();
And check if in your User Model:
protected $fillable = [
'name','mobile', 'national_code', 'city', 'province', 'address',
'postcode', 'active', 'avatar',
];

Laravel - propery does not exist on this collection instance

So I have a small Laravel car project and I have two separate tables, cars table and vehicle_infos table.
Here is my CarsController#store:
public function store(Request $request)
{
$this->validate($request, [
'naslov' => 'required',
'marka' => 'required',
'model' => 'required',
/*
'kubikaza' => 'required',
'zamajac' => 'required',
'potrosnja' => 'required',
'karoserija' => 'required',
'kilometraza' => 'required',
'godiste' => 'required',
'gorivo' => 'required',
'vlasnistvo' => 'required',
'kilovata' => 'required',
'konjska_snaga' => 'required',
'emisiona_klasa' => 'required',
'pogon' => 'required',
'mjenjac' => 'required',
'br_brzina_mjenjaca' => 'required',
'velicina_felni' => 'required',
'posjeduje_gume' => 'required',
'br_vrata' => 'required',
'br_sjedista' => 'required',
'str_volana' => 'required',
'klima' => 'required',
'boja_spolj' => 'required',
'boja_unutrasnj' => 'required',
'materijal_unutrasnj' => 'required',
'registracija' => 'required',
'ostecenje' => 'required',
'zamjena' => 'required',
'sigurnost' => 'required',
'oprema' => 'required',
'stanje' => 'required',
'nacin_finansiranja' => 'required',
'nacin_prodaje' => 'required',
'cijena' => 'required',
'vrsta_cijene' => 'required',
'opis_oglasa' => 'required',
//'fotografije' => 'required',
//'kontakt' => 'required',
*/
]);
/*
// Handle File Upload
if($request->hasFile('fotografije')){
// Get filename with the extension
$filenameWithExt = $request->file('fotografije')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('fotografije')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('fotografije')->storeAs('public/slike_oglasa', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
*/
$images=array();
if($files=$request->file('fotografije')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('slike_oglasa',$name);
$images[]=$name;
}
}
$car = new Car;
$car->naslov = $request->input('naslov');
$car->marka = $request->input('marka');
$car->model = $request->input('model');
$car->kubikaza = $request->input('kubikaza');
$car->zamajac = $request->input('zamajac');
//$car->potrosnja = $request->input('potrosnja');
$car->karoserija = $request->input('karoserija');
$car->godiste = $request->input('godiste');
$car->kilometraza = $request->input('kilometraza');
$car->gorivo = $request->input('gorivo');
$car->vlasnistvo = $request->input('vlasnistvo');
$car->kilovata = $request->input('kilovata');
$car->konjska_snaga = $request->input('konjska_snaga');
$car->emisiona_klasa = $request->input('emisiona_klasa');
$car->pogon = $request->input('pogon');
$car->mjenjac = $request->input('mjenjac');
$car->br_brzina_mjenjaca = $request->input('br_brzina_mjenjaca');
$car->velicina_felni = $request->input('velicina_felni');
$car->posjeduje_gume = $request->input('posjeduje_gume');
$car->br_vrata = $request->input('br_vrata');
$car->br_sjedista = $request->input('br_sjedista');
$car->str_volana = $request->input('str_volana');
$car->klima = $request->input('klima');
$car->boja_spolj = $request->input('boja_spolj');
$car->boja_unutrasnj = $request->input('boja_unutrasnj');
$car->materijal_unutrasnj = $request->input('materijal_unutrasnj');
$car->registracija = $request->input('registracija');
$car->ostecenje = $request->input('ostecenje');
$car->zamjena = $request->input('zamjena');
$car->sigurnost = implode(',', $request->input('sigurnost'));
$car->oprema = implode(',', $request->input('oprema'));
$car->stanje = implode(',', $request->input('stanje'));
$car->nacin_finansiranja = $request->input('nacin_finansiranja');
$car->nacin_prodaje = $request->input('nacin_prodaje');
$car->cijena = $request->input('cijena');
$car->vrsta_cijene = $request->input('vrsta_cijene');
$car->opis_oglasa = $request->input('opis_oglasa');
//$car->user_id = 1;
$car->user_id = auth()->user()->id;
$car->fotografije = implode("|", $images);
$car->trajanje_oglasa = $request->input('trajanje_oglasa');
$car->placeni_status = $request->input('placeni_status');
if($car->trajanje_oglasa == 30){
$car->to_datum_isteka = Carbon::now()->addDays(30);
} else {
$car->to_datum_isteka = Carbon::now()->addDays(60);
}
if($car->placeni_status == 0){
$car->po_datum_isteka = Carbon::now();
} else if($car->placeni_status == 1) {
$car->po_datum_isteka = Carbon::now()->addDays(7);
} else if($car->placeni_status == 2) {
$car->po_datum_isteka = Carbon::now()->addDays(14);
} else if($car->placeni_status == 3) {
$car->po_datum_isteka = Carbon::now()->addDays(21);
}
//$car->kontakt = $request->input('kontakt');
$car->save();
$vehicleinfo = new VehicleInfo;
//$vehicleinfo->urban = "Urban";
$vehicleinfo->car_id = $car->id;
$vehicleinfo->save();
//ukupno oglasa od strane usera, skladistenje u ads table
$ad = new Ad;
$ad->car_id = $car->id;
//$ad->user_id = 1;
$ad->user_id = auth()->user()->id;
$ad->save();
return redirect('/cars');
}
My model Car.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Car extends Model
{
protected $table = "cars";
protected $primaryKey = "id";
protected $fillable = [
'naslov', 'marka', 'model', 'kubikaza', 'zamajac', 'karoserija', 'godiste', 'kilometraza', 'br_brzina_mjenjaca',
'gorivo', 'vlasnistvo', 'kilovata', 'konjska_snaga', 'emisiona_klasa', 'pogon', 'mjenjac', 'br_vrata', 'velicina_felni', 'posjeduje_gume',
'br_sjedista', 'str_volana', 'klima', 'boja_spolj', 'boja_unutrasnj', 'materijal_unutrasnj', 'registracija', 'ostecenje',
'zamjena', 'sigurnost', 'oprema', 'stanje', 'nacin_finansiranja', 'nacin_prodaje', 'cijena', 'vrsta_cijene', 'opis_oglasa', 'fotografije'
];
public function user(){
return $this->belongsTo(User::class);
}
public function vehicleinfo(){
return $this->hasMany(VehicleInfo::class);
}
public function ad(){
return $this->hasMany(Ad::class);
}
}
My model VehicleInfo.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class VehicleInfo extends Model
{
protected $table = 'vehicle_infos';
protected $primaryKey = 'id';
protected $fillable = ['car_id', 'urban', 'extra_urban', 'combined', 'length', 'width', 'height'];
public function car(){
return $this->belongsTo(Car::class);
}
}
So my car_id from vehicle_infos table is coming from public function store() of the CarsController and it automatically storing new data in vehicle_infos and that is okay.
But when I do php artisan tinker to check my relations it shows me error. So here is what I do in php artisan tinker:
First step: $car = App\Car::find(4); and it shows me all data for that car, then $car->vehicleinfo and it shows me all data from vehicle_infos table with car_id of 4.
But when I do $car->vehicleinfo->urban ,for example, it shows me this error Exception with message 'Property [urban] does not exist on this collection instance.'. What do I do wrong? Please help me it frustating.
Basing from the structure of your model, cars has many vehicleinfo. Doing $cars->vehicleinfo will give you a collection instance since it hasMany of it. Doing $car->vehicleinfo->urban will not work since there is no urban property on a collection instance.
Try doing:
$cars->vehicleinfo->first()->urban;
Or if you want all the urban properties of all the related vehicle infos:
$cars->vehicleinfo->pluck('urban') // returns an array of all the urban properties found
See Laravel Collections for more methods.
Hello can you try to replace find() by find()->first() or replace $car->vehicleinfo->urban by $car->vehicleinfo[´urban’]
And my eyes see this line urban is commented is normal the collection of instance urban not exist if you push data with the line commented. Normally sql will gold you you’re column urban is required no ?
This line have comment for urban
`
$vehicleinfo = new VehicleInfo;
//$vehicleinfo->urban = "Urban";
$vehicleinfo->car_id = $car->id;
$vehicleinfo->save(); `

Categories