Laravel 3:Looking for Explanation How to Use the Model

What is the proper way to use the Model in Laravel?

Repositories is a smart decision to you. But why?
Basically, repositories is a 'gateway' between your application and your storage.

With repositories, you'll find your 'database queries' in a single place.

Let's think about the model Articles.
Instead of use a static instance of Articles all the times that you need to use it (Articles::find(), Articles::all(), etc), just create a repository of Articles.

Inject this repo in your controller (e.g.), and use 'features' storaged in your ArticleRepository.

What do you mean?
Let's consider a repository of Articles. What I'll use many times in my app of Articles model? I need select all, select by id, insert, update, delete. Basically these 'stuffs'. So, if I have all this stuffs in a place?

class ArticleRepository {

public function all(){}
public function getById($id){}
public function insert($data){}
public function update($data){}
public function delete($id){}

}

Inject this ArticleRepository in your controller. To do this, read a about IoC Container here: http://laravel.com/docs/5.0/container

The construct in your controller will be like this:

public function __construct(ArticleRepository $articles)
{
$this->articles = $articles;
}

Once all, when you need get all Articles in your controller, just do:

public function index()
{
$articles = $this->articles->all();
return View::make('articles.index')->with(['articles' => $articles]);
}

With this practice, you have a clean application with testables controllers and a beautiful organization and design. ;)

Look, I tried to be as didactic as possible to you understand the concept. The use of repositories is not only a way to do. So I let the links in the comments. And let other references here as well.

I'm sure you will understand quickly.

Success in learning! :)

https://laracasts.com/search?q=repositories&q-where=lessons

http://ryantablada.com/post/the-repository-pattern-in-action

http://culttt.com/2014/03/17/eloquent-tricks-better-repositories/

http://culttt.com/2013/07/15/how-to-structure-testable-controllers-in-laravel-4/

How to set Where condition in my model definition in laravel

Add following method in your customer model.

public function scopeCustomers($query)
{
return $query->where('role_id', 5);
}

and fetch records like this:-

$customers = Customer::customers();

How to obtain three level model data laravel

It looks like you're not really grasping the concept of relations yet. Also, I'd advise you to look into route model binding :) What you basically want to be doing is:

public function getEducationalBackground($id) 
{
$user = User::find($id);
return $user->educationalBackgrounds()->with('educationalAwards')->get();
}

Also, when you're pretty sure that whenever you want to use backgrounds, you also want to use the awards, you can add the with(...) to the model definition like so:

class EducationalBackground extends Model
{
...
protected $with = ['educationalAwards'];
}

That way, you can simplify your controller method to:

public function getEducationalBackground($id) 
{
$user = User::find($id);
return $user->educationalBackgrounds;
}

How to retrieve data through model?

Just having a quick look at your relationships it looks like you could create a hasManyThrough relationship on the order Model.

public function Photo {
return $this->hasManyThrough('App\OrderPhoto', 'App\Photo')
}

You may need to add the table keys to make it work

This will allow you to do:

Order::with("Photo")->get();

You can see more details here https://laravel.com/docs/5.5/eloquent-relationships#has-many-through

Update

Try this

public function Photo {
return $this->hasManyThrough('App\Photo', 'App\OrderPhoto', 'Order_id', 'Photos_id', 'id', 'id')
}

It is a little hard to get my head around your DB structure with this info but you should hopefully be able to work it out. This may also help

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_hasManyThrough

Is there a explanation for my model function can't be used in my controller?

Auth::user() returns an object of type Illuminate\Contracts\Auth\Authenticatable which does not implement isFollowing

Option 1 : You can add @var annotation to specify the type of your object

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

class Profile extends Controller

{
public function show($id)
{
$user = User::where('id', $id)->firstOrFail();
/** @var User $me */
$me = Auth::user();
$is_edit_profile = (Auth::id() == $user->id);
$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
}
}

Option 2 : You can extends the Auth facade by creating a new facade with the expected return type :

namespace App\Extensions\Facades;

use App\Models\User;

/**
* @method static User user()
*/
class Auth extends \Illuminate\Support\Facades\Auth
{

}

And then you can use this facade instead of the previous one

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Extensions\Facades\Auth;
use App\Models\User;

class Profile extends Controller

{
public function show($id)
{
$user = User::where('id', $id)->firstOrFail();
$me = Auth::user();
$is_edit_profile = (Auth::id() == $user->id);
$is_follow_button = (!$is_edit_profile) && (!$me->isFollowing($user));
return view('profile', ['user' => $user, 'is_edit_profile' => $is_edit_profile, 'is_follow_button' => $is_follow_button]);
}
}

In eloquent : How to get a model that has the count of a related model exactly n with a condition?

Per the Eloquent Relationships Documentation you can do something like this:

Course
::whereHas('students',function($q){
$q->where('gender','Female');
}, '=', 20)
->get()

how to generate short link in laravel framework and relation with 3 model

That's easily done through routing.
So in your route file it will be something like this...

Route::get('music/{slug}',  ['as' => 'music', 'uses' => 'music\MusicController@music']);

Then anything that comes in to your domain as https://yourdomain.com/music/slug will go to that music controller and you can process what slug is, then grab the model stuff you need and shoot them to a view.

If you really want nothing but short urls meaning https://yourdomain.com/shorturl you'll have to put the wild card at the root. This could have some undesired effects though if you don't have your routes set up in order. I'd probably put some middleware on the group too to process the shortUrl code and then send it on through to the controller where you can handle bringing back the model and shooting them to a view.



Related Topics



Leave a reply



Submit