Answers for "laravel update models"

PHP
6

laravel create or update

// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Models\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);
Posted by: Guest on September-27-2020
0

laravel model update table

ModelName::whereId($id)->update($request->all());
Posted by: Guest on August-02-2021
0

laravel find query

use App\Models\Destination;
use App\Models\Flight;

return Destination::addSelect(['last_flight' => Flight::select('name')
    ->whereColumn('destination_id', 'destinations.id')
    ->orderBy('arrived_at', 'desc')
    ->limit(1)
])->get();
Posted by: Guest on October-08-2020
1

laravel model::query

// Eloquent's Model::query() returns the query builder

Model::where()->get();
// Is the same as 
Model::query()->where()->get();

Model::query();
// Can be useful to add query conditions based on certain requirements

// See https://stackoverflow.com/questions/51517203/what-is-the-meaning-of-eloquents-modelquery
Posted by: Guest on October-06-2020
0

Laravel inserting and updating models

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Flight;
use Illuminate\Http\Request;

class FlightController extends Controller
{
    /**
     * Store a new flight in the database.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // Validate the request...

        $flight = new Flight;

        $flight->name = $request->name;

        $flight->save();
    }
}
Posted by: Guest on July-24-2021

Browse Popular Code Answers by Language