Skip to content
Advertisement

Can’t get a list of streets to create a card [closed]

 public function create()
    {
       $streets = street::get();
       return view('dashboard.house.created');
    }

 public function store(Request $request)
    {
$this->validate($request, [
            'home_number' => 'required',
            'passport' => 'required',
            'option_1' => 'required',
            'street_id' => 'required',
            'option_2' => 'required',
            'option_3' => 'required',
            'garage' => 'required',
            'apartments' => 'required',
            's_full' => 'required',
            's_life' => 'required',
            's_nlife' => 'required',
            'status' => 'required',
            'year_create' => 'required',
        ]);

        $homes = Houses::query()->create([
                'home_number' => $request->get('home_number'),
                'passport' => $request->get('passport'),
                'option_1' => $request->get('option_1'),
                'option_2' => $request->get('option_2'),
                'street_id' => $request->get('street_id'),
                'option_3' => $request->get('option_3'),
                'garage' => $request->get('garage'),
                'apartments' => $request->get('apartments'),
                's_full' => $request->get('s_full'),
                's_life' => $request->get('s_life'),
                's_nlife' => $request->get('s_nlife'),
                'status' => $request->get('status'),
                'year_create' => $request->get('year_create'),
            ]
        );
        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $homes->addMediaFromRequest('image')->toMediaCollection('images');
        }

I can’t fill out the card at home when creating. The list of streets appears, but when you select a street, and further fill in all the fields, the page is simply refreshed without entering the data into the database. Where I made a mistake in the code, please tell me!

UPD: Now I can not go to the page in the admin panel with a list of houses, writes

ErrorException
Attempt to read property "name" on null (View: D:laravel-testedproject1resourcesviewsDashboardhousemain.blade.php)

The error is highlighted on this line (this is where you get the street and house number).

<td class="w-2/3 py-5">{{$home->street->name}}, {{$home->home_number}}</td>

created.blade.php

 <div>
                        <label for="street">Street</label>
                        <select name="street_id" class="w-full">
                            <option selected>Choose street</option>
                            @foreach($streets as $street)
                                <option value="{{$street->id}}">{{$street->name}}</option>
                            @endforeach
                        </select>
                    </div>

Advertisement

Answer

You can validate the street in your store method, i’m assuming the table in the database is called streets and has a primary key id

$this->validate($request, [
            'home_number' => 'required',
            //...
            'street_id" => 'exists:streets,id',
            //...
            'year_create' => 'required',
        ]);

Maybe street_id is not present in the attribute $fillable of your House model and it’s not set to '*'. Change the way you save the model

$homes = new Houses();
$home->home_number = $request->get('home_number');
$home->passport = $request->get('passport');
$home->option_1 = $request->get('option_1');
$home->option_2 = $request->get('option_2');
$home->street_id = $request->get('street_id');
$home->option_3 = $request->get('option_3');
$home->garage = $request->get('garage');
$home->apartments = $request->get('apartments');
$home->s_full = $request->get('s_full');
$home->s_life = $request->get('s_life');
$home->s_nlife = $request->get('s_nlife');
$home->status = $request->get('status');
$home->year_create = $request->get('year_create');
$home->save();

if ($request->hasFile('image') && $request->file('image')->isValid()) {
    $homes->addMediaFromRequest('image')->toMediaCollection('images'); 
}

or set in Houses class

protected $fillable = ['*'];
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement